use std::fs;
use std::path::{Path, PathBuf};
use serde_json::json;
use crate::error::GuardError;
use crate::policy::{check_init_idea_protection, GuardPolicy};
#[derive(Debug, Clone)]
pub struct ScaffoldReport {
pub root: PathBuf,
pub generated_files: Vec<PathBuf>,
}
pub fn init_prototype(idea: &str, target_dir: &Path) -> Result<ScaffoldReport, GuardError> {
let clean_idea = idea.trim();
if clean_idea.is_empty() {
return Err(GuardError::Config(
"prototype idea must not be empty".to_string(),
));
}
check_init_idea_protection(clean_idea)?;
if fs::symlink_metadata(target_dir)
.map(|m| m.file_type().is_symlink())
.unwrap_or(false)
{
crate::policy::check_canonical_path_protection(target_dir)?;
}
fs::create_dir_all(target_dir)?;
let spec_path = target_dir.join("SPEC.md");
let tasks_path = target_dir.join("TASKS.md");
let skill_path = target_dir.join("SKILL.md");
let mcp_path = target_dir.join(".mcp.json");
let policy_path = target_dir.join("monaa-guard.toml");
let spec_md = format!(
"# SPEC.md — Spec-Locked Prototype Contract\n\n\
## Product Idea\n{clean_idea}\n\n\
## Non-Negotiable Architecture Rules\n\
- Build the smallest working vertical slice cleanly before adding scope.\n\
- Maintain 100% BYOK custody: zero LLM API keys leave your local machine.\n\
- Route MCP traffic through `monaa-guard proxy --upstream <cmd>`.\n\
- Never execute destructive shell patterns (`rm -rf`) or expose `.env` secrets.\n\n\
## Acceptance Criteria\n\
1. Core workflow for **{clean_idea}** passes automated tests cleanly.\n\
2. `monaa-guard doctor` confirms `<50 us` local wire latency and `0` leaked keys.\n"
);
let tasks_md = format!(
"# TASKS.md — Ordered Build Checklist\n\n\
- [ ] Milestone 1: Review `SPEC.md` boundaries and `monaa-guard.toml` wire policy.\n\
- [ ] Milestone 2: Write failing tests for **{clean_idea}** before coding.\n\
- [ ] Milestone 3: Implement minimal modular code until all tests pass.\n\
- [ ] Milestone 4: Run formatter, linter, tests, and `monaa-guard doctor`.\n"
);
let skill_md = format!(
"---\nname: spec-locked-prototype-builder\ndescription: Deterministic execution guide for building \"{clean_idea}\" without scope drift.\n---\n\n\
# Execution Protocol\n\
1. Read `SPEC.md` and `TASKS.md` before modifying any source file.\n\
2. Keep MCP tools routed through `monaa-guard proxy --upstream <cmd>`.\n\
3. Never read `.env` credentials or run unapproved destructive commands.\n\
4. Check off each item in `TASKS.md` only after running verification tests.\n"
);
let mcp_json = serde_json::to_string_pretty(&json!({
"mcpServers": {
"monaa-guard": {
"command": "monaa-guard",
"args": [
"proxy",
"--upstream",
"npx -y @modelcontextprotocol/server-filesystem ."
]
}
}
}))
.map_err(|e| GuardError::Config(format!("failed serializing .mcp.json: {e}")))?;
let policy_toml = GuardPolicy::default().to_toml_string()?;
fs::write(&spec_path, spec_md)?;
fs::write(&tasks_path, tasks_md)?;
fs::write(&skill_path, skill_md)?;
fs::write(&mcp_path, format!("{mcp_json}\n"))?;
fs::write(&policy_path, policy_toml)?;
Ok(ScaffoldReport {
root: target_dir.to_path_buf(),
generated_files: vec![spec_path, tasks_path, skill_path, mcp_path, policy_path],
})
}