apm-core 0.1.15

Core library for APM — a git-native project manager for parallel AI coding agents.
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
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
use schemars::JsonSchema;

pub struct FieldEntry {
    pub toml_path: String,
    pub type_name: String,
    pub default: Option<String>,
    pub description: Option<String>,
    pub enum_variants: Option<Vec<String>>,
    pub required: bool,
}

pub fn schema_entries<T: JsonSchema>() -> Vec<FieldEntry> {
    let root = schemars::schema_for!(T);
    let defs = &root.definitions;
    walk_object(&root.schema, defs, "")
}

pub fn render_schema<T: JsonSchema>() -> String {
    let entries = schema_entries::<T>();
    if entries.is_empty() {
        return String::new();
    }
    let path_w = entries.iter().map(|e| e.toml_path.len()).max().unwrap_or(0);
    let type_w = entries.iter().map(|e| e.type_name.len()).max().unwrap_or(0);
    entries
        .iter()
        .map(|e| {
            let mut line =
                format!("{:<path_w$}  {:<type_w$}", e.toml_path, e.type_name);
            if let Some(ref d) = e.default {
                line.push_str(&format!("  [default: {}]", d));
            }
            if let Some(ref desc) = e.description {
                line.push_str(&format!("  # {}", desc));
            }
            if let Some(ref variants) = e.enum_variants {
                line.push_str(&format!("  ({})", variants.join(" | ")));
            }
            line
        })
        .collect::<Vec<_>>()
        .join("\n")
}

// ── internals ──────────────────────────────────────────────────────────────

fn walk_object(
    schema: &SchemaObject,
    defs: &schemars::Map<String, Schema>,
    prefix: &str,
) -> Vec<FieldEntry> {
    let Some(ref obj) = schema.object else {
        return vec![];
    };
    let required_set = &obj.required;

    let mut props: Vec<(&String, &Schema)> = obj.properties.iter().collect();
    props.sort_by_key(|(k, _)| k.as_str());

    let mut result = Vec::new();
    for (field_name, field_schema) in props {
        let Schema::Object(field_obj) = field_schema else {
            continue;
        };

        let path = if prefix.is_empty() {
            field_name.clone()
        } else {
            format!("{}.{}", prefix, field_name)
        };
        let required = required_set.contains(field_name.as_str());

        // Metadata lives on the field schema (wrapper), not the definition.
        let description = field_obj
            .metadata
            .as_ref()
            .and_then(|m| m.description.clone());
        let default_val = field_obj
            .metadata
            .as_ref()
            .and_then(|m| m.default.as_ref())
            .map(fmt_default);

        // Structural information may live in a $ref definition.
        let structural = resolve_structural(field_obj, defs);

        // Fall back to the definition's metadata when the field wrapper has none.
        let description = description.or_else(|| {
            structural
                .metadata
                .as_ref()
                .and_then(|m| m.description.clone())
        });
        let default_val = default_val.or_else(|| {
            structural
                .metadata
                .as_ref()
                .and_then(|m| m.default.as_ref())
                .map(fmt_default)
        });

        let entries = classify(structural, defs, &path, required, description, default_val);
        result.extend(entries);
    }
    result
}

/// Follow a direct `$ref` or an `allOf: [{$ref: ...}]` wrapper to get the
/// schema that carries type structure (instance_type, object, array, etc.).
fn resolve_structural<'a>(
    schema: &'a SchemaObject,
    defs: &'a schemars::Map<String, Schema>,
) -> &'a SchemaObject {
    // Direct $ref
    if let Some(ref r) = schema.reference {
        if let Some(Schema::Object(def)) = defs.get(ref_name(r)) {
            return def;
        }
    }
    // allOf with a single $ref (schemars wraps typed fields that also carry defaults)
    if let Some(ref subs) = schema.subschemas {
        if let Some(ref all_of) = subs.all_of {
            if all_of.len() == 1 {
                if let Schema::Object(inner) = &all_of[0] {
                    if let Some(ref r) = inner.reference {
                        if let Some(Schema::Object(def)) = defs.get(ref_name(r)) {
                            return def;
                        }
                    }
                }
            }
        }
    }
    schema
}

fn ref_name(r: &str) -> &str {
    r.strip_prefix("#/definitions/").unwrap_or(r)
}

fn classify(
    schema: &SchemaObject,
    defs: &schemars::Map<String, Schema>,
    path: &str,
    required: bool,
    description: Option<String>,
    default_val: Option<String>,
) -> Vec<FieldEntry> {
    // ── string enum ────────────────────────────────────────────────────────
    if let Some(ref enum_vals) = schema.enum_values {
        let variants: Vec<String> = enum_vals
            .iter()
            .filter_map(|v| {
                if let serde_json::Value::String(s) = v {
                    Some(s.clone())
                } else {
                    None
                }
            })
            .collect();
        return vec![FieldEntry {
            toml_path: path.to_string(),
            type_name: "string".to_string(),
            default: default_val,
            description,
            enum_variants: if variants.is_empty() { None } else { Some(variants) },
            required,
        }];
    }

    // ── anyOf / oneOf (untagged enums, Option<T>) ──────────────────────────
    if let Some(ref subs) = schema.subschemas {
        let variants = subs.any_of.as_deref().or(subs.one_of.as_deref());
        if let Some(vs) = variants {
            let non_null: Vec<&Schema> =
                vs.iter().filter(|s| !is_null_schema(s)).collect();

            if non_null.len() == 1 {
                // Option<T> — unwrap and classify the inner type.
                let Schema::Object(inner) = non_null[0] else {
                    return vec![];
                };
                let structural = resolve_structural(inner, defs);
                return classify(structural, defs, path, required, description, default_val);
            } else if non_null.len() > 1 {
                // True union (e.g. SatisfiesDeps: bool | string).
                let type_names: Vec<String> = non_null
                    .iter()
                    .filter_map(|s| {
                        if let Schema::Object(obj) = s {
                            let resolved = resolve_structural(obj, defs);
                            Some(instance_type_name(resolved))
                        } else {
                            None
                        }
                    })
                    .collect();
                return vec![FieldEntry {
                    toml_path: path.to_string(),
                    type_name: type_names.join(" | "),
                    default: default_val,
                    description,
                    enum_variants: None,
                    required,
                }];
            }
        }
    }

    // ── array ──────────────────────────────────────────────────────────────
    if let Some(ref arr) = schema.array {
        if let Some(ref items) = arr.items {
            let item_structural = match items {
                SingleOrVec::Single(s) => {
                    if let Schema::Object(obj) = s.as_ref() {
                        resolve_structural(obj, defs)
                    } else {
                        return vec![];
                    }
                }
                SingleOrVec::Vec(v) => {
                    if let Some(Schema::Object(obj)) = v.first() {
                        resolve_structural(obj, defs)
                    } else {
                        return vec![];
                    }
                }
            };

            let is_struct = item_structural
                .object
                .as_ref()
                .map(|o| !o.properties.is_empty())
                .unwrap_or(false);

            if is_struct {
                return walk_object(item_structural, defs, &format!("{}[]", path));
            } else {
                return vec![FieldEntry {
                    toml_path: path.to_string(),
                    type_name: format!("list-of-{}", instance_type_name(item_structural)),
                    default: default_val,
                    description,
                    enum_variants: None,
                    required,
                }];
            }
        }
        return vec![];
    }

    // ── nested struct or map ───────────────────────────────────────────────
    if let Some(ref obj) = schema.object {
        if !obj.properties.is_empty() {
            // Nested struct — recurse (no FieldEntry for the container).
            return walk_object(schema, defs, path);
        }
        if obj.additional_properties.is_some() {
            // HashMap — emit one entry, do not recurse into values.
            return vec![FieldEntry {
                toml_path: path.to_string(),
                type_name: "map".to_string(),
                default: default_val,
                description,
                enum_variants: None,
                required,
            }];
        }
    }

    // ── scalar ─────────────────────────────────────────────────────────────
    if let Some(ref it) = schema.instance_type {
        let type_name = match it {
            SingleOrVec::Single(t) => scalar_name(t),
            SingleOrVec::Vec(types) => {
                let non_null: Vec<_> = types
                    .iter()
                    .filter(|t| **t != InstanceType::Null)
                    .collect();
                non_null
                    .first()
                    .map(|t| scalar_name(t))
                    .unwrap_or_else(|| "null".to_string())
            }
        };
        return vec![FieldEntry {
            toml_path: path.to_string(),
            type_name,
            default: default_val,
            description,
            enum_variants: None,
            required,
        }];
    }

    vec![]
}

fn is_null_schema(schema: &Schema) -> bool {
    match schema {
        Schema::Object(obj) => matches!(
            &obj.instance_type,
            Some(SingleOrVec::Single(t)) if **t == InstanceType::Null
        ),
        Schema::Bool(_) => false,
    }
}

fn instance_type_name(schema: &SchemaObject) -> String {
    if let Some(ref it) = schema.instance_type {
        match it {
            SingleOrVec::Single(t) => scalar_name(t),
            SingleOrVec::Vec(types) => {
                let non_null: Vec<_> = types
                    .iter()
                    .filter(|t| **t != InstanceType::Null)
                    .collect();
                non_null
                    .first()
                    .map(|t| scalar_name(t))
                    .unwrap_or_else(|| "null".to_string())
            }
        }
    } else {
        "unknown".to_string()
    }
}

fn scalar_name(t: &InstanceType) -> String {
    match t {
        InstanceType::String => "string".to_string(),
        InstanceType::Integer => "integer".to_string(),
        InstanceType::Boolean => "bool".to_string(),
        InstanceType::Number => "number".to_string(),
        InstanceType::Null => "null".to_string(),
        InstanceType::Array => "array".to_string(),
        InstanceType::Object => "object".to_string(),
    }
}

fn fmt_default(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        _ => serde_json::to_string(v).unwrap_or_default(),
    }
}

// ── tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Config, WorkflowConfig};

    #[test]
    fn agents_max_concurrent_has_default_3() {
        let entries = schema_entries::<Config>();
        let entry = entries
            .iter()
            .find(|e| e.toml_path == "agents.max_concurrent")
            .expect("agents.max_concurrent not found");
        assert_eq!(entry.default.as_deref(), Some("3"));
        assert!(!entry.required);
    }

    #[test]
    fn project_name_is_required() {
        let entries = schema_entries::<Config>();
        let entry = entries
            .iter()
            .find(|e| e.toml_path == "project.name")
            .expect("project.name not found");
        assert!(entry.required);
    }

    #[test]
    fn workflow_states_uses_array_notation() {
        let entries = schema_entries::<Config>();
        assert!(
            entries.iter().any(|e| e.toml_path.starts_with("workflow.states[].")),
            "no entry with toml_path starting with 'workflow.states[].'"
        );
    }

    #[test]
    fn completion_strategy_has_enum_variants() {
        let entries = schema_entries::<Config>();
        let entry = entries
            .iter()
            .find(|e| e.toml_path == "workflow.states[].transitions[].completion")
            .expect("workflow.states[].transitions[].completion not found");
        let variants = entry
            .enum_variants
            .as_ref()
            .expect("enum_variants should be Some");
        assert!(variants.contains(&"none".to_string()), "missing 'none'");
        assert!(variants.contains(&"pr".to_string()), "missing 'pr'");
        assert!(variants.contains(&"merge".to_string()), "missing 'merge'");
        assert!(variants.contains(&"pull".to_string()), "missing 'pull'");
        assert!(
            variants.contains(&"pr_or_epic_merge".to_string()),
            "missing 'pr_or_epic_merge'"
        );
    }

    #[test]
    fn satisfies_deps_has_union_type_name() {
        let entries = schema_entries::<WorkflowConfig>();
        let entry = entries
            .iter()
            .find(|e| e.toml_path == "states[].satisfies_deps")
            .expect("states[].satisfies_deps not found");
        assert_eq!(entry.type_name, "bool | string");
        assert!(entry.enum_variants.is_none());
    }

    #[test]
    fn render_schema_contains_agents_max_concurrent() {
        let output = render_schema::<Config>();
        assert!(!output.is_empty());
        assert!(
            output.contains("agents.max_concurrent"),
            "render_schema output does not contain 'agents.max_concurrent'"
        );
    }
}