jerrycan 0.6.10

The AI-native Rust backend platform: framework, CLI, and MCP server. https://jerrycan.cc
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
//! tools/call dispatch — the MCP twins of the CLI commands.

use super::design::Design;
use super::{checkpipe, genroute, mounting, questions, scaffold};
use serde_json::{Value, json};
use std::path::{Path, PathBuf};

fn root_from(args: &Value) -> PathBuf {
    args["directory"]
        .as_str()
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."))
}

fn err_payload(msg: impl Into<String>) -> (bool, Value) {
    (true, json!({ "error": msg.into() }))
}

pub fn dispatch(name: &str, args: &Value) -> (bool, Value) {
    match name {
        "jerrycan_docs_search" => {
            let query = args["query"].as_str().unwrap_or("");
            let limit = args["limit"].as_u64().unwrap_or(5) as usize;
            (
                false,
                json!({ "results": super::docsidx::search(query, limit) }),
            )
        }
        "jerrycan_docs_get" => {
            let page = args["page"].as_str().unwrap_or("");
            match super::docsidx::get(page, args["anchor"].as_str()) {
                Some(md) => (false, json!({ "markdown": md })),
                None => (
                    true,
                    json!({ "error": format!("unknown docs page `{page}`") }),
                ),
            }
        }

        "jerrycan_design" => {
            let Some(draft) = args.get("draft").filter(|d| !d.is_null()) else {
                let template = include_str!("../../embedded/designs/todo-api.design.json");
                return (
                    false,
                    json!({
                        "status": "questions",
                        "questions": [{
                            "id": "/",
                            "question": format!(
                                "Provide a structured `draft` conforming to design-schema.json. Be specific: modules, entities+fields, endpoints with operation_id/method/path/success/errors. Worked example:\n{template}"
                            ),
                        }],
                        "next_step": "author the draft from the requirements, then call jerrycan_design again with it",
                    }),
                );
            };
            let mut design: Design = match serde_json::from_value(draft.clone()) {
                Ok(d) => d,
                Err(e) => {
                    return (
                        false,
                        json!({
                            "status": "questions",
                            "questions": [{ "id": "/", "question": format!("draft does not parse against design-schema.json: {e}") }],
                            "next_step": "fix the draft and call jerrycan_design again",
                        }),
                    );
                }
            };
            // Match `from_path`: the tenant entity's own detail route is stored
            // (and generated) as `/{tenant_fk}`, so the guard path-verifies it (#78).
            design.normalize_tenant_detail_routes();
            let qs = questions::validate(&design);
            if !qs.is_empty() {
                return (
                    false,
                    json!({
                        "status": "questions",
                        "questions": qs,
                        "next_step": "answer each question by fixing the draft, then call jerrycan_design again",
                    }),
                );
            }
            let path = args["revision_of"]
                .as_str()
                .map(PathBuf::from)
                .unwrap_or_else(|| PathBuf::from("design.json"));
            if let Err(e) = std::fs::write(&path, scaffold::canonical_design_json(&design)) {
                return err_payload(format!("cannot write {}: {e}", path.display()));
            }
            let abs = path.canonicalize().unwrap_or(path);
            (
                false,
                json!({
                    "status": "complete",
                    "design": serde_json::to_value(&design).expect("design serializes"),
                    "design_path": abs.display().to_string(),
                    "next_step": "call jerrycan_scaffold with this design_path and a target directory",
                }),
            )
        }

        "jerrycan_scaffold" => {
            let (Some(design_path), Some(directory)) =
                (args["design_path"].as_str(), args["directory"].as_str())
            else {
                return err_payload("design_path and directory are required");
            };
            let design = match Design::from_path(Path::new(design_path)) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            let qs = questions::validate(&design);
            if !qs.is_empty() {
                return (
                    true,
                    json!({ "error": "design is incomplete", "questions": qs }),
                );
            }
            // #27: reject a fatal design-shape conflict before scaffolding, the
            // same rule the CLI runs — the MCP twin must not die mid-scaffold on
            // a half-written tree with a raw SQLite error.
            if let Some(conflict) = questions::design_conflict(&design) {
                return (
                    true,
                    json!({ "error": conflict.message, "code": conflict.code, "hint": conflict.hint }),
                );
            }
            match scaffold::scaffold(Path::new(directory), &design) {
                Ok(mut created) => {
                    // db apps ship a derived schema.json contract.
                    match super::schema::write_schema(Path::new(directory), &design) {
                        Ok(Some(rel)) => created.push(rel),
                        Ok(None) => {}
                        Err(e) => return err_payload(e),
                    }
                    (
                        false,
                        json!({
                            "created": created,
                            "next_step": "implement the handler stubs (see jerrycan_list_routes), then jerrycan_check",
                        }),
                    )
                }
                Err(e) => err_payload(e),
            }
        }

        "jerrycan_generate" => {
            let root = root_from(args);
            let design_path = root.join("design.json");
            let mut design = match Design::from_path(&design_path) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            let kind = args["kind"].as_str().unwrap_or("");
            let path = args["path"].as_str().unwrap_or("");
            match kind {
                "route" | "subroute" => {
                    let routes_before = genroute::route_map(&design).len();
                    if let Some(slice) = args.get("design_slice").filter(|s| !s.is_null()) {
                        let module: super::design::ModuleDesign =
                            match serde_json::from_value(slice.clone()) {
                                Ok(m) => m,
                                Err(e) => {
                                    return err_payload(format!(
                                        "design_slice does not parse: {e}"
                                    ));
                                }
                            };
                        if kind == "route" && module.name != path {
                            return err_payload(format!(
                                "design_slice.name `{}` does not match path `{path}` — set path to the module the slice replaces (slices replace the WHOLE module)",
                                module.name
                            ));
                        }
                        if kind == "route" {
                            design.modules.retain(|m| m.name != module.name);
                            design.modules.push(module);
                        } else {
                            let Some((parent_path, _)) = path.rsplit_once('/') else {
                                return err_payload("subroute path must be parent/child");
                            };
                            let Some(parent) =
                                genroute::module_by_path_mut(&mut design, parent_path)
                            else {
                                return err_payload(format!(
                                    "parent module `{parent_path}` not found"
                                ));
                            };
                            parent.subroutes.retain(|s| s.name != module.name);
                            parent.subroutes.push(module);
                        }
                    }
                    // A merged design_slice can carry the tenant module's own
                    // `/{id}` detail route un-normalized (it's parsed by
                    // `from_value`, not `from_path`); re-normalize so the slice
                    // path can't reopen the #78 leak.
                    design.normalize_tenant_detail_routes();
                    let qs = questions::validate(&design);
                    if !qs.is_empty() {
                        return (
                            true,
                            json!({ "error": "design would become incomplete", "questions": qs }),
                        );
                    }
                    if genroute::module_by_path(&design, path).is_none() {
                        return err_payload(format!(
                            "module `{path}` not in design.json — pass a design_slice or edit the design first"
                        ));
                    }
                    if let Err(e) =
                        std::fs::write(&design_path, scaffold::canonical_design_json(&design))
                    {
                        return err_payload(e.to_string());
                    }
                    let top_name = path.split('/').next().expect("nonempty");
                    let top = design
                        .modules
                        .iter()
                        .find(|m| m.name == top_name)
                        .expect("validated above");
                    let mode = genroute::GenMode {
                        db: design.wants_db(),
                        auth: design.wants_auth(),
                    };
                    // #69: use the reporting variant so agent-added `mod`/`use`
                    // wiring dropped from the tool-owned lib.rs/mod.rs is surfaced
                    // loudly on the MCP channel too — never silently lost (the CLI
                    // already does this; the MCP twin must match).
                    let (created, dropped) = match genroute::write_module_reporting(
                        &root.join("crates/routes"),
                        top,
                        mode,
                        &design,
                    ) {
                        Ok(cd) => cd,
                        Err(e) => return err_payload(e),
                    };
                    let modified = match mounting::regenerate(&root, &design) {
                        Ok(m) => m,
                        Err(e) => return err_payload(e),
                    };
                    let routes_after = genroute::route_map(&design).len();
                    let mut next_step = format!(
                        "implement crates/routes/{top_name}/src/handlers.rs, then jerrycan_check"
                    );
                    if routes_after < routes_before {
                        next_step.push_str(&format!(
                            " — warning: route count dropped {routes_before}{routes_after}; a partial design_slice REPLACES the whole module (stale agent files are not deleted)"
                        ));
                    }
                    // Append each dropped-wiring warning to next_step so an agent
                    // reading only that field can't miss it.
                    for (file, lines) in &dropped {
                        next_step.push_str(&format!(
                            " — warning: regenerating tool-owned {file} dropped {} agent-added line(s) NOT preserved ({}); re-home this wiring in an AGENT-owned file (handlers.rs, or a module's model.rs) — see `jerrycan_docs_get page=database`. lib.rs/mod.rs are tool-owned and rewritten on every generate.",
                            lines.len(),
                            lines.join(", ")
                        ));
                    }
                    let mut payload = json!({
                        "created": created,
                        "modified": modified,
                        "next_step": next_step,
                    });
                    // Structured mirror of the CLI `--json` envelope: an array of
                    // { file, dropped_lines }. Omitted entirely when nothing dropped.
                    if !dropped.is_empty() {
                        payload["warnings"] = Value::Array(
                            dropped
                                .iter()
                                .map(
                                    |(file, lines)| json!({ "file": file, "dropped_lines": lines }),
                                )
                                .collect(),
                        );
                    }
                    (false, payload)
                }
                "dependency" => {
                    let Some(module) = args["module"].as_str() else {
                        return err_payload("`module` is required for kind=dependency");
                    };
                    if let Err(e) = genroute::add_dependency(&mut design, module, path) {
                        return err_payload(e);
                    }
                    if let Err(e) =
                        std::fs::write(&design_path, scaffold::canonical_design_json(&design))
                    {
                        return err_payload(e.to_string());
                    }
                    (
                        false,
                        json!({
                            "created": [],
                            "modified": ["design.json"],
                            "next_step": format!("define `{path}` in the module's deps.rs configure() hook"),
                        }),
                    )
                }
                other => err_payload(format!("unknown kind `{other}`")),
            }
        }

        "jerrycan_check" => {
            let root = root_from(args);
            let design = match Design::from_path(&root.join("design.json")) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            // `full_report` is the MCP alias for the CLI's `--full-report` flag.
            let no_fail_fast = args["no_fail_fast"].as_bool().unwrap_or(false)
                || args["full_report"].as_bool().unwrap_or(false);
            match checkpipe::run_all(&root, &design, args["module"].as_str(), no_fail_fast) {
                Ok(report) => (
                    false,
                    serde_json::to_value(&report).expect("report serializes"),
                ),
                Err(env) => err_payload(env),
            }
        }

        "jerrycan_list_routes" => {
            let root = root_from(args);
            match Design::from_path(&root.join("design.json")) {
                Ok(design) => {
                    // Match the CLI lister: the tool-owned member routes (#107)
                    // are registered at `App::build` but live outside the
                    // design's endpoint table — append them so the listing
                    // shows the full live surface (empty without tenancy).
                    let mut routes = genroute::route_map(&design);
                    routes.extend(genroute::implicit_member_routes(&design));
                    (false, json!({ "routes": routes }))
                }
                Err(e) => err_payload(e),
            }
        }

        "jerrycan_gen_tests" => {
            let root = root_from(args);
            let design = match Design::from_path(&root.join("design.json")) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            // No `module`: mirror the CLI's bare `gen-tests` — every
            // endpoint-bearing module's suite plus the jobs suite once (#159).
            // The single-module path below stays byte-identical.
            let Some(module) = args["module"].as_str() else {
                let all = match super::testgen::write_all_acceptance(&root, &design) {
                    Ok(a) => a,
                    Err(e) => return err_payload(e),
                };
                let super::testgen::AllAcceptance {
                    tests_created,
                    expected_failing,
                    packages,
                    has_jobs,
                } = all;
                let next_step = if packages.is_empty() {
                    "nothing to generate — the design declares no endpoints and no jobs".to_string()
                } else {
                    let run = packages
                        .iter()
                        .map(|p| format!("cargo test -p {p}"))
                        .collect::<Vec<_>>()
                        .join(" && ");
                    let implement = match (packages.len() > usize::from(has_jobs), has_jobs) {
                        (true, true) => "implement handlers + job tasks",
                        (true, false) => "implement handlers",
                        (false, _) => "implement job tasks",
                    };
                    format!(
                        "run the tests to see them fail ({run}), {implement}, iterate until green"
                    )
                };
                return (
                    false,
                    json!({
                        "tests_created": tests_created,
                        "expected_failing": expected_failing,
                        "next_step": next_step,
                    }),
                );
            };
            match super::testgen::write_acceptance(&root, &design, module) {
                Ok((rel, count)) => {
                    // The declared jobs get their own tool-owned acceptance tests
                    // (direct task-fn calls); like the HTTP ones they fail on the
                    // stubs, so they count toward `expected_failing` too.
                    match super::jobsgen::write_jobs_acceptance(&root, &design) {
                        Ok(jobs) => {
                            let mut tests_created = vec![rel];
                            let mut count = count;
                            // Jobs are top-level (not per-module): their acceptance
                            // tests are written ONCE, so `jobs_count` is added to the
                            // total exactly once.
                            let has_jobs = jobs.is_some();
                            if let Some((jobs_rel, jobs_count)) = jobs {
                                tests_created.push(jobs_rel);
                                count += jobs_count;
                            }
                            // With jobs, the jobs acceptance tests live in package
                            // `jobs`; point the operator at them too.
                            let next_step = if has_jobs {
                                format!(
                                    "run the tests to see them fail, implement crates/routes/{module}/src/handlers.rs and crates/jobs/src/*.rs job tasks, iterate until green (jerrycan test --module {module} && cargo test -p jobs)"
                                )
                            } else {
                                format!(
                                    "run the tests to see them fail, implement crates/routes/{module}/src/handlers.rs, iterate until green (jerrycan test --module {module})"
                                )
                            };
                            (
                                false,
                                json!({
                                    "tests_created": tests_created,
                                    "expected_failing": count,
                                    "next_step": next_step,
                                }),
                            )
                        }
                        Err(e) => err_payload(e),
                    }
                }
                Err(e) => err_payload(e),
            }
        }
        "jerrycan_package" => {
            let root = root_from(args);
            let design = match Design::from_path(&root.join("design.json")) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            // The frozen contract takes ONE singular `target`; map it to the
            // matching bool and run the same orchestration as the CLI.
            let (docker, k8s, systemd, binary) = match args["target"].as_str() {
                Some("docker") => (true, false, false, false),
                Some("k8s") => (false, true, false, false),
                Some("systemd") => (false, false, true, false),
                Some("binary") => (false, false, false, true),
                other => {
                    return err_payload(format!(
                        "`target` must be one of docker|binary|k8s|systemd, got {other:?}"
                    ));
                }
            };
            match super::package::run_package(&root, &design, docker, k8s, systemd, binary) {
                Ok((artifacts, sbom)) => (
                    false,
                    json!({
                        "artifacts": artifacts,
                        "sbom": sbom,
                        "next_step": "deploy with your own tooling (kubectl apply -f deploy/k8s.yaml, docker build, scp the binary + systemd unit)",
                    }),
                ),
                Err(e) => err_payload(e),
            }
        }

        "jerrycan_schema" => {
            let root = root_from(args);
            let design = match Design::from_path(&root.join("design.json")) {
                Ok(d) => d,
                Err(e) => return err_payload(e),
            };
            if !design.wants_db() {
                return err_payload(
                    "this app has no `db` dependency — there is no schema contract to derive",
                );
            }
            // Derive on a throwaway runtime (dispatch is sync), as `db migrate` does.
            let runtime = match tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
            {
                Ok(r) => r,
                Err(e) => return err_payload(e.to_string()),
            };
            match runtime.block_on(super::schema::derive_schema(&root, &design)) {
                // Return the contract JSON directly as the structured payload.
                Ok(contract) => (
                    false,
                    serde_json::to_value(&contract).expect("contract serializes"),
                ),
                Err(e) => err_payload(e),
            }
        }

        other => (true, json!({ "error": format!("unknown tool `{other}`") })),
    }
}