selfware 0.6.1

Your personal AI workshop — software you own, software that lasts
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Interactive setup wizard and template-based config generation.

use anyhow::Result;

use crate::ui::style::Glyphs;

/// Print helper that respects the global `--quiet` flag.
macro_rules! wizard_print {
    () => {
        if !crate::output::is_quiet() {
            print!("\n");
        }
    };
    ($($arg:tt)*) => {
        if !crate::output::is_quiet() {
            print!($($arg)*);
            print!("\n");
        }
    };
}

pub(crate) fn run_init_wizard(template: Option<String>, scaffold: bool) -> Result<()> {
    use std::io::{self, BufRead, IsTerminal, Write};
    use std::path::PathBuf;

    // If a template is provided, skip the interactive wizard
    if let Some(ref tmpl) = template {
        return write_template_config(tmpl);
    }

    // The wizard reads every answer from stdin; without a terminal each
    // read_line hits EOF and silently selects the default, persisting an
    // all-defaults localhost config (shadowing any working global config)
    // that the user never asked for. Refuse instead — mirrors the headless
    // fail-fast guards on the chat/run entry paths.
    ensure_interactive_stdin(std::io::stdin().is_terminal())?;

    if scaffold {
        run_scaffold_interview()?;
    }

    wizard_print!();
    wizard_print!(
        "{} Welcome to Selfware! Let's set up your workspace.",
        Glyphs::seedling()
    );
    wizard_print!();

    // Detect project type
    let project_type = if std::path::Path::new("Cargo.toml").exists() {
        "Rust (Cargo.toml)"
    } else if std::path::Path::new("package.json").exists() {
        "Node.js (package.json)"
    } else if std::path::Path::new("pyproject.toml").exists()
        || std::path::Path::new("setup.py").exists()
    {
        "Python (pyproject.toml)"
    } else if std::path::Path::new("go.mod").exists() {
        "Go (go.mod)"
    } else {
        "Unknown"
    };
    wizard_print!("  Detecting project type... Found: {}", project_type);
    wizard_print!();

    // Step 1: Endpoint
    wizard_print!("Step 1/4: API Endpoint");
    wizard_print!("  Where should Selfware connect?");
    wizard_print!("  [1] Local (http://127.0.0.1:1234/v1) - LM Studio, Ollama, vLLM");
    wizard_print!("  [2] OpenAI-compatible API (https://api.openai.com/v1)");
    wizard_print!("  [3] Custom endpoint");
    print!("  > ");
    io::stdout().flush()?;
    let mut choice = String::new();
    io::stdin().lock().read_line(&mut choice)?;
    let endpoint = match choice.trim() {
        "2" => "https://api.openai.com/v1".to_string(),
        "3" => {
            print!("  Enter endpoint URL: ");
            io::stdout().flush()?;
            let mut url = String::new();
            io::stdin().lock().read_line(&mut url)?;
            url.trim().to_string()
        }
        _ => "http://127.0.0.1:1234/v1".to_string(),
    };
    wizard_print!();

    // Step 2: Model
    wizard_print!("Step 2/4: Model");
    let default_model = if endpoint.contains("openai") {
        "gpt-4"
    } else {
        "qwen3-coder"
    };
    print!("  Which model should Selfware use? [{}]: ", default_model);
    io::stdout().flush()?;
    let mut model = String::new();
    io::stdin().lock().read_line(&mut model)?;
    let model = if model.trim().is_empty() {
        default_model.to_string()
    } else {
        model.trim().to_string()
    };
    wizard_print!();

    // Step 2.5: API key (optional) — stored in the OS keyring, never the config.
    wizard_print!("Step: API Key (optional)");
    wizard_print!("  If your endpoint needs an API key (e.g. OpenRouter), paste it now.");
    wizard_print!("  It is stored in your OS keyring — NOT written to the config file.");
    wizard_print!("  Leave blank to skip (you can set SELFWARE_API_KEY later).");
    print!("  > ");
    io::stdout().flush()?;
    let mut api_key = String::new();
    io::stdin().lock().read_line(&mut api_key)?;
    let api_key = api_key.trim();
    if !api_key.is_empty() {
        match crate::config::save_api_key_to_keyring(&endpoint, api_key) {
            Ok(()) => wizard_print!("  {} API key saved to your OS keyring.", Glyphs::bloom()),
            Err(e) => wizard_print!(
                "  {} Could not save to keyring ({}). Set SELFWARE_API_KEY=<key> in your environment instead.",
                Glyphs::frost(),
                e
            ),
        }
    }
    wizard_print!();

    // Step 3: Allowed paths
    wizard_print!("Step 3/4: Allowed Paths");
    wizard_print!("  Which directories can Selfware access?");
    wizard_print!("  [1] Current directory only (.)");
    wizard_print!("  [2] Home directory (~)");
    wizard_print!("  [3] Custom paths");
    print!("  > ");
    io::stdout().flush()?;
    let mut path_choice = String::new();
    io::stdin().lock().read_line(&mut path_choice)?;
    let allowed_paths = match path_choice.trim() {
        "2" => {
            let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
            allowed_paths_toml([dir_allow_entry(&home)])
        }
        "3" => {
            print!("  Enter paths (comma-separated): ");
            io::stdout().flush()?;
            let mut paths = String::new();
            io::stdin().lock().read_line(&mut paths)?;
            allowed_paths_toml(
                paths
                    .split(',')
                    .map(str::trim)
                    .filter(|p| !p.is_empty())
                    .map(custom_allow_entry),
            )
        }
        _ => {
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            allowed_paths_toml([dir_allow_entry(&cwd)])
        }
    };
    wizard_print!();

    // Step 4: Execution mode
    wizard_print!("Step 4/4: Execution Mode");
    wizard_print!("  How should Selfware handle file changes?");
    wizard_print!("  [1] Normal - Ask before every edit (safest)");
    wizard_print!("  [2] AutoEdit - Auto-approve file edits, confirm commands");
    wizard_print!("  [3] YOLO - Auto-approve everything (use with caution!)");
    print!("  > ");
    io::stdout().flush()?;
    let mut mode_choice = String::new();
    io::stdin().lock().read_line(&mut mode_choice)?;
    let mode = match mode_choice.trim() {
        "2" => "autoedit",
        "3" => "yolo",
        _ => "normal",
    };
    wizard_print!();

    // Write config
    write_config_file(&endpoint, &model, mode, &allowed_paths)
}

/// Refuse to run the interactive wizard without a terminal on stdin: every
/// `read_line` would hit EOF and silently select the default answer,
/// persisting an all-defaults localhost config the user never asked for.
fn ensure_interactive_stdin(is_terminal: bool) -> Result<()> {
    if !is_terminal {
        anyhow::bail!(
            "`selfware init` is an interactive wizard and needs a terminal on stdin. \
             For a non-interactive setup use `selfware init --template <rust|python|node|minimal>`, \
             or re-run in a terminal."
        );
    }
    Ok(())
}

/// Quote `s` as a TOML basic-string literal. Backslashes, double quotes, and
/// control characters are escaped, so Windows paths (`C:\Users\...`) don't
/// produce invalid TOML escape sequences (`\U`, `\s`) in the generated config.
fn toml_quote(s: &str) -> String {
    use std::fmt::Write as _;
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            c if (c as u32) < 0x20 || c as u32 == 0x7f => {
                let _ = write!(out, "\\u{:04X}", c as u32);
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Allow-list entry for a concrete directory: `<dir>/**`. The path validator
/// matches allow-list entries as exact globs, so a bare directory would match
/// only the directory itself and DENY every file inside it — the descendant
/// glob is what actually grants the project the user picked.
fn dir_allow_entry(dir: &std::path::Path) -> String {
    format!("{}/**", dir.display())
}

/// User-supplied custom path → allow-list entry. Entries that already carry
/// glob metacharacters are left alone; plain directories get the descendant
/// glob (`/**`) appended, for the same reason as [`dir_allow_entry`].
fn custom_allow_entry(raw: &str) -> String {
    let trimmed = raw.trim();
    if trimmed.contains(['*', '?', '[']) {
        return trimmed.to_string();
    }
    // Strip trailing separators so we don't emit `foo//**`, but never turn a
    // Windows drive root (`C:\`) into a relative-looking `C:/**`.
    let base = trimmed.trim_end_matches(['/', '\\']);
    if base.len() == 2 && base.ends_with(':') {
        format!("{}**", trimmed)
    } else {
        format!("{}/**", base)
    }
}

/// Render allow-list entries as a TOML array literal with each entry quoted
/// via [`toml_quote`].
fn allowed_paths_toml<I, S>(entries: I) -> String
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let quoted: Vec<String> = entries
        .into_iter()
        .map(|e| toml_quote(e.as_ref()))
        .collect();
    format!("[{}]", quoted.join(", "))
}

/// Ask structured questions about the project to build, then scaffold it into
/// the current directory via the interview-driven template engine.
fn run_scaffold_interview() -> Result<()> {
    use std::io::IsTerminal;

    if !std::io::stdin().is_terminal() {
        wizard_print!(
            "  {} --scaffold needs an interactive terminal; skipping.",
            Glyphs::frost()
        );
        return Ok(());
    }
    let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
    let ctx = crate::interview::run_interview("Scaffold a new project", &cwd)?;
    match crate::templates::scaffold_from_context(&ctx, &cwd) {
        Ok(files) => {
            wizard_print!("  {} Scaffolded {} files:", Glyphs::bloom(), files.len());
            for f in &files {
                wizard_print!("    {}", f);
            }
        }
        Err(e) => wizard_print!("  {} Could not scaffold: {}", Glyphs::frost(), e),
    }
    Ok(())
}

fn write_template_config(template: &str) -> Result<()> {
    let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));

    // Scaffold project files for known language templates
    match template {
        "rust" | "python" | "node" | "nodejs" | "typescript" => {
            wizard_print!("  {} Using '{}' template...", Glyphs::gear(), template);

            let lang_key = match template {
                "node" | "nodejs" | "typescript" => "nodejs",
                other => other,
            };

            let project_name = cwd
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("my-project")
                .to_string();

            let engine = crate::templates::TemplateEngine::new();
            let opts = crate::templates::ScaffoldOptions {
                description: format!("A {} project scaffolded by Selfware", template),
                framework: None,
                with_ci: true,
                with_tests: true,
                qa_profile: "standard".into(),
            };

            match engine.scaffold_project(lang_key, &project_name, &cwd, &opts) {
                Ok(files) => {
                    wizard_print!("  {} Scaffolded {} files:", Glyphs::bloom(), files.len());
                    for f in &files {
                        wizard_print!("    {}", f);
                    }
                }
                Err(e) => {
                    wizard_print!("  {} Could not scaffold project: {}", Glyphs::frost(), e);
                }
            }
        }
        "minimal" => {
            wizard_print!("  {} Using 'minimal' template...", Glyphs::gear());
        }
        other => {
            anyhow::bail!(
                "Unknown template '{}'. Available templates: rust, python, node, nodejs, typescript, minimal",
                other
            );
        }
    }

    let (endpoint, model, mode, allowed_paths) = match template {
        "rust" | "python" | "node" | "nodejs" | "typescript" => (
            "http://127.0.0.1:1234/v1".to_string(),
            "qwen3-coder".to_string(),
            "normal",
            allowed_paths_toml([dir_allow_entry(&cwd)]),
        ),
        _ => (
            "http://127.0.0.1:1234/v1".to_string(),
            "qwen3-coder".to_string(),
            "normal",
            "[\"./**\"]".to_string(),
        ),
    };

    write_config_file(&endpoint, &model, mode, &allowed_paths)
}

/// Build the generated `selfware.toml` body. Execution mode is deliberately
/// NOT written as a live key — it's `#[serde(skip)]` (runtime-only, so a repo
/// config can't silently enable auto-approval), so emitting it would be a dead
/// key that misleads the user. A comment documents how to select it per run.
fn build_config_content(endpoint: &str, model: &str, allowed_paths: &str) -> String {
    format!(
        r#"# Selfware Configuration
# Generated by `selfware init`

endpoint = "{}"
model = "{}"

# Token limits. `context_length` MUST match your server's real context window
# (vLLM --max-model-len, LM Studio's context slider, Ollama num_ctx); raise it
# when your model serves more. `max_tokens` is the per-response output budget
# and must fit inside context_length with room to spare for conversation.
# Leaving these unset is NOT safe for models selfware doesn't recognize: the
# context window falls back to a conservative 32k while max_tokens defaults
# to 64k, which doesn't fit.
context_length = 32768
max_tokens = 8192

# Execution mode is chosen per RUN, not stored here — a config file (this one
# lives in the repo) must never be able to silently enable auto-approval.
# Start Selfware in your chosen mode with a flag or env var:
#   Normal (ask first): selfware
#   AutoEdit:           selfware -m auto-edit   (or SELFWARE_MODE=auto-edit)
#   YOLO (auto all):    selfware -y             (or SELFWARE_MODE=yolo)

[safety]
allowed_paths = {}

[agent]
# token_budget defaults to 60% of context_length — set explicitly to override
# token_budget = 19660
"#,
        endpoint, model, allowed_paths
    )
}

/// Probe whether something is listening on the endpoint's host:port.
/// TCP-connect only — no HTTP, no async — so it is safe to call from the
/// sync wizard. A `false` result means "nothing answered in time", i.e. the
/// endpoint is almost certainly dead; it does not prove the URL path is
/// right, only that a server is (not) there.
fn probe_endpoint_tcp(endpoint: &str, timeout: std::time::Duration) -> bool {
    use std::net::{TcpStream, ToSocketAddrs};

    let Ok(url) = url::Url::parse(endpoint) else {
        return false;
    };
    let Some(host) = url.host_str() else {
        return false;
    };
    let Some(port) = url.port_or_known_default() else {
        return false;
    };
    let Ok(mut addrs) = (host, port).to_socket_addrs() else {
        return false;
    };
    addrs.any(|addr| TcpStream::connect_timeout(&addr, timeout).is_ok())
}

fn write_config_file(endpoint: &str, model: &str, mode: &str, allowed_paths: &str) -> Result<()> {
    use std::path::PathBuf;

    // Write the workspace config to `./selfware.toml` so the loader finds it
    // before falling back to the global config directory.
    let config_path = std::env::current_dir()
        .unwrap_or_else(|_| PathBuf::from("."))
        .join("selfware.toml");

    // Check if config already exists
    if config_path.exists() {
        use std::io::{self, BufRead, Write};

        wizard_print!(
            "  {} Configuration already exists at {}",
            Glyphs::frost(),
            config_path.display()
        );
        print!("  Overwrite? [y/N]: ");
        io::stdout().flush()?;
        let mut answer = String::new();
        io::stdin().lock().read_line(&mut answer)?;
        if !answer.trim().eq_ignore_ascii_case("y") {
            wizard_print!("  Aborted. Existing configuration preserved.");
            return Ok(());
        }
    }

    let content = build_config_content(endpoint, model, allowed_paths);

    // Never write a config selfware itself would reject: validate the exact
    // TOML body the way the loader would after reading it from disk, and fail
    // loudly instead of persisting garbage.
    crate::config::Config::validate_generated_toml(&content)?;

    // Probe the endpoint before declaring success — a config pointing at a
    // dead endpoint is the most common first-run failure. TCP-connect only
    // (no HTTP, no async runtime), so this is safe in the sync wizard.
    let endpoint_reachable = probe_endpoint_tcp(endpoint, std::time::Duration::from_secs(2));

    std::fs::write(&config_path, &content)?;
    wizard_print!(
        "  {} Configuration saved to {}",
        Glyphs::bloom(),
        config_path.display()
    );

    // Trust the config we just wrote. It was created by the user, in this
    // directory, by an explicit `init` — so the untrusted-checkout
    // restriction (which silently resets allowed_paths/hooks/MCP to defaults)
    // must not strip it on the next run. This is the same store
    // `selfware trust` writes (~/.selfware/trusted_repos).
    match crate::config::trust::add_trusted_config(&config_path) {
        Ok(()) => wizard_print!(
            "  {} Trusted this config — its [safety] settings take effect on the next run.",
            Glyphs::bloom()
        ),
        Err(e) => {
            wizard_print!(
                "  {} Could not record repo trust ({}). Run `selfware trust` in this directory,",
                Glyphs::frost(),
                e
            );
            wizard_print!(
                "     otherwise the untrusted-checkout protection resets allowed_paths/hooks/MCP to defaults."
            );
        }
    }

    // The new project-local file shadows any global config in this directory —
    // name that explicitly so a working global setup doesn't look "lost".
    if let Some(home_config) = dirs::home_dir().map(|h| h.join(".config/selfware/config.toml")) {
        if home_config.is_file() {
            wizard_print!(
                "  {} Note: {} takes precedence over your global config ({}) in this directory.",
                Glyphs::frost(),
                config_path.display(),
                home_config.display()
            );
        }
    }

    if endpoint_reachable {
        wizard_print!("  {} Endpoint {} is reachable.", Glyphs::bloom(), endpoint);
    } else {
        wizard_print!();
        wizard_print!(
            "  {} WARNING: could not reach {} — the config was written UNVERIFIED.",
            Glyphs::frost(),
            endpoint
        );
        wizard_print!(
            "     Start your LLM server (or fix the endpoint), then verify with `selfware llm-doctor`."
        );
    }
    wizard_print!();
    // Echo the run command for the mode the user picked — the mode isn't
    // persisted (see the config comment), so tell them exactly how to get it.
    match mode {
        "yolo" => wizard_print!(
            "  {} You chose YOLO — start it with `selfware -y` (mode isn't saved in config, for safety).",
            Glyphs::sprout()
        ),
        "autoedit" | "auto-edit" | "auto_edit" => wizard_print!(
            "  {} You chose AutoEdit — start it with `selfware -m auto-edit`.",
            Glyphs::sprout()
        ),
        _ => wizard_print!(
            "  {} Run `selfware` to start your workshop!",
            Glyphs::sprout()
        ),
    }

    Ok(())
}

#[cfg(test)]
#[path = "../../tests/unit/cli/init_wizard/init_wizard_test.rs"]
mod tests;