voc 0.1.1

Voc Agent — the Rust terminal cockpit for Vibecrafted
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
use anyhow::Context;
use std::collections::BTreeMap;
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str::FromStr;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LaunchKind {
    Workflow,
    Research,
    Review,
    Marbles,
}

impl LaunchKind {
    pub fn label(self) -> &'static str {
        match self {
            LaunchKind::Workflow => "workflow",
            LaunchKind::Research => "research",
            LaunchKind::Review => "review",
            LaunchKind::Marbles => "marbles",
        }
    }

    pub fn human_title(self) -> &'static str {
        match self {
            LaunchKind::Workflow => "Workflow",
            LaunchKind::Research => "Research swarm",
            LaunchKind::Review => "Review",
            LaunchKind::Marbles => "Marbles loop",
        }
    }

    pub fn human_description(self) -> &'static str {
        match self {
            LaunchKind::Workflow => {
                "Best default. Examine the surface, plan the cut, then implement."
            }
            LaunchKind::Research => "Send a research pass first when the shape is still unclear.",
            LaunchKind::Review => {
                "Audit an existing surface for risk, regressions, and weak claims."
            }
            LaunchKind::Marbles => {
                "Run convergence loops when the code works but still lies or drifts."
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LaunchRuntime {
    Headless,
    #[default]
    Terminal,
    Visible,
}

impl LaunchRuntime {
    pub fn label(self) -> &'static str {
        match self {
            LaunchRuntime::Headless => "headless",
            LaunchRuntime::Terminal => "terminal",
            LaunchRuntime::Visible => "visible",
        }
    }

    pub fn cycle(self) -> Self {
        match self {
            LaunchRuntime::Headless => LaunchRuntime::Terminal,
            LaunchRuntime::Terminal => LaunchRuntime::Visible,
            LaunchRuntime::Visible => LaunchRuntime::Headless,
        }
    }
}

impl FromStr for LaunchRuntime {
    type Err = anyhow::Error;

    fn from_str(raw: &str) -> Result<Self, Self::Err> {
        match raw.trim().to_ascii_lowercase().as_str() {
            "headless" => Ok(Self::Headless),
            "terminal" => Ok(Self::Terminal),
            "visible" => Ok(Self::Visible),
            other => Err(anyhow::anyhow!(
                "unsupported runtime: {other} (expected headless|terminal|visible)"
            )),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LaunchRequest {
    pub kind: LaunchKind,
    pub agent: String,
    pub prompt: String,
    pub runtime: LaunchRuntime,
    pub root: Option<PathBuf>,
    pub terminal_binary: Option<PathBuf>,
    pub env: BTreeMap<String, OsString>,
    pub count: Option<u32>,
    pub depth: Option<u32>,
    pub session_name: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LaunchCommand {
    pub program: PathBuf,
    pub args: Vec<OsString>,
    pub env: BTreeMap<String, OsString>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LaunchReadinessProbe {
    pub program: PathBuf,
    pub args: Vec<OsString>,
    pub env: BTreeMap<String, OsString>,
    pub session_name: String,
}

impl LaunchCommand {
    pub fn command_line(&self) -> String {
        let mut parts = vec![self.program.to_string_lossy().into_owned()];
        parts.extend(
            self.args
                .iter()
                .map(|value| value.to_string_lossy().into_owned()),
        );
        parts.join(" ")
    }

    pub fn spawn(&self) -> anyhow::Result<std::process::Child> {
        let mut command = Command::new(&self.program);
        command.args(&self.args);
        command.envs(&self.env);
        command.stdin(Stdio::inherit());
        command.stdout(Stdio::inherit());
        command.stderr(Stdio::inherit());
        command.spawn().context("failed to spawn launch command")
    }

    pub fn spawn_detached(&self) -> anyhow::Result<std::process::Child> {
        let mut command = Command::new(&self.program);
        command.args(&self.args);
        command.envs(&self.env);
        command.stdin(Stdio::null());
        command.stdout(Stdio::null());
        command.stderr(Stdio::null());
        command.spawn().context("failed to spawn launch command")
    }

    pub fn spawn_interactive_with_stderr(&self) -> anyhow::Result<std::process::Child> {
        let mut command = Command::new(&self.program);
        command.args(&self.args);
        command.envs(&self.env);
        command.stdin(Stdio::inherit());
        command.stdout(Stdio::inherit());
        command.stderr(Stdio::piped());
        command.spawn().context("failed to spawn launch command")
    }

    pub fn readiness_probe(&self) -> Option<LaunchReadinessProbe> {
        let session_name = self
            .args
            .windows(2)
            .find(|pair| pair.first().is_some_and(|value| value == "--session"))
            .and_then(|pair| pair.get(1))
            .map(|value| value.to_string_lossy().into_owned())?;
        // The launch command may carry a top-level `--config-dir <dir>` flag so
        // that zellij talks to the repo-local namespace under
        // `<root>/config/zellij`. The readiness probe MUST inspect the same
        // namespace, otherwise it can succeed/fail against a completely
        // different socket directory and report nonsense to the operator.
        let config_dir = self
            .args
            .windows(2)
            .find(|pair| pair.first().is_some_and(|value| value == "--config-dir"))
            .and_then(|pair| pair.get(1))
            .cloned();
        let mut args: Vec<OsString> = Vec::new();
        if let Some(dir) = config_dir {
            args.push("--config-dir".into());
            args.push(dir);
        }
        args.push("list-sessions".into());
        args.push("--short".into());
        args.push("--no-formatting".into());
        Some(LaunchReadinessProbe {
            program: self.program.clone(),
            args,
            env: self.env.clone(),
            session_name,
        })
    }
}

impl LaunchReadinessProbe {
    pub fn is_session_visible(&self) -> anyhow::Result<bool> {
        let output = Command::new(&self.program)
            .args(&self.args)
            .envs(&self.env)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .context("failed to run zellij readiness probe")?;
        if !output.status.success() {
            // A non-zero exit with diagnostic stderr is a real probe error
            // (bad flags, broken config, permission denied, missing socket
            // root, etc.) and must surface to the operator overlay rather
            // than collapsing into "session not visible". A non-zero exit
            // with empty stderr is treated as a benign "no sessions yet"
            // signal so we keep polling. zellij itself prints the
            // "No active zellij sessions found." message on stderr, so the
            // empty-stderr branch is only reached for stripped/quiet
            // implementations or wrappers that intentionally silence
            // diagnostics.
            let stderr = String::from_utf8_lossy(&output.stderr);
            if !stderr.trim().is_empty() {
                anyhow::bail!(
                    "zellij readiness probe exited with {}: {}",
                    output.status,
                    stderr.trim()
                );
            }
            return Ok(false);
        }
        let stdout = String::from_utf8_lossy(&output.stdout);
        Ok(stdout.lines().any(|line| line.trim() == self.session_name))
    }
}

pub fn build_launch_command(deck: impl AsRef<Path>, request: &LaunchRequest) -> LaunchCommand {
    let deck = deck.as_ref();
    if matches!(
        request.runtime,
        LaunchRuntime::Terminal | LaunchRuntime::Visible
    ) {
        return build_terminal_launch_command(deck, request);
    }
    build_deck_launch_command(deck, request)
}

fn build_deck_launch_command(deck: &Path, request: &LaunchRequest) -> LaunchCommand {
    let mut args: Vec<OsString> = vec![request.kind.label().into()];
    match request.kind {
        LaunchKind::Workflow | LaunchKind::Review => {
            args.push(request.agent.clone().into());
            if !request.prompt.trim().is_empty() {
                args.push("--prompt".into());
                args.push(request.prompt.clone().into());
            }
        }
        LaunchKind::Research => {
            if !request.prompt.trim().is_empty() {
                args.push("--prompt".into());
                args.push(request.prompt.clone().into());
            }
        }
        LaunchKind::Marbles => {
            args.push(request.agent.clone().into());
            args.push("--count".into());
            args.push(request.count.unwrap_or(3).to_string().into());
            args.push("--depth".into());
            args.push(request.depth.unwrap_or(3).to_string().into());
            if !request.prompt.trim().is_empty() {
                args.push("--prompt".into());
                args.push(request.prompt.clone().into());
            }
        }
    }
    args.push("--runtime".into());
    args.push(request.runtime.label().into());
    if let Some(root) = request.root.as_ref() {
        args.push("--root".into());
        args.push(root.as_os_str().to_os_string());
    }
    LaunchCommand {
        program: deck.to_path_buf(),
        args,
        env: request.env.clone(),
    }
}

fn build_terminal_launch_command(deck: &Path, request: &LaunchRequest) -> LaunchCommand {
    let zellij_layout = build_zellij_layout_string(deck, request);
    let zellij_config_dir = resolved_zellij_config_dir(request.root.as_deref());

    let mut args = Vec::new();

    // Stable session name (when provided) goes before the subcommand so the
    // operator can `zellij attach <name>` from another terminal and so future
    // healthcheck paths can target the named socket.
    if let Some(name) = request.session_name.as_deref() {
        args.push("--session".into());
        args.push(name.into());
    }

    // We are running zellij in-place.
    args.push("options".into());

    if let Some(config_dir) = zellij_config_dir {
        args.push("--config-dir".into());
        args.push(config_dir.into_os_string());
    }

    args.push("--layout-string".into());
    args.push(zellij_layout.into());

    LaunchCommand {
        program: request
            .terminal_binary
            .clone()
            .unwrap_or_else(|| PathBuf::from("zellij")),
        args,
        env: request.env.clone(),
    }
}

fn build_zellij_layout_string(deck: &Path, request: &LaunchRequest) -> String {
    let mut layout = format!(
        "layout {{ tab name={} focus=true {{ pane name={} focus=true command={} ",
        kdl_quote(&format!("Operator {}", request.kind.human_title())),
        kdl_quote("launch"),
        kdl_quote("bash"),
    );
    if let Some(root) = request.root.as_deref() {
        layout.push_str(&format!("cwd={} ", kdl_quote(&root.to_string_lossy())));
    }
    layout.push_str("{ args ");
    layout.push_str(&kdl_quote("-lc"));
    layout.push(' ');
    layout.push_str(&kdl_quote(&build_pane_shell_command(deck, request)));
    layout.push_str(" } } } }");
    layout
}

fn build_pane_shell_command(deck: &Path, request: &LaunchRequest) -> String {
    let mut parts = Vec::new();
    if let Some(config_dir) = resolved_zellij_config_dir(request.root.as_deref()) {
        parts.push(format!(
            "export ZELLIJ_CONFIG_DIR={}",
            shell_quote(&config_dir.to_string_lossy())
        ));
    }
    for (key, value) in &request.env {
        parts.push(format!(
            "export {}={}",
            shell_name(key),
            shell_quote(&value.to_string_lossy())
        ));
    }
    parts.extend(tooling_profile_snippets());
    parts.push(format!(
        "exec {}",
        shell_join(deck, &build_deck_launch_command(deck, request).args)
    ));
    parts.join("; ")
}

fn resolved_zellij_config_dir(root: Option<&Path>) -> Option<PathBuf> {
    if let Some(explicit) = env::var_os("ZELLIJ_CONFIG_DIR").filter(|value| !value.is_empty()) {
        return Some(PathBuf::from(explicit));
    }
    let root = root?;
    let repo_config_dir = root.join("config/zellij");
    repo_config_dir
        .join("config.kdl")
        .is_file()
        .then_some(repo_config_dir)
}

fn shell_join(program: &Path, args: &[OsString]) -> String {
    let mut parts = Vec::with_capacity(args.len() + 1);
    parts.push(shell_quote(&program.to_string_lossy()));
    parts.extend(
        args.iter()
            .map(|value| shell_quote(&value.to_string_lossy())),
    );
    parts.join(" ")
}

fn shell_quote(raw: &str) -> String {
    format!("'{}'", raw.replace('\'', "'\"'\"'"))
}

fn shell_name(raw: &str) -> String {
    raw.chars()
        .filter(|ch| ch.is_ascii_alphanumeric() || *ch == '_')
        .collect()
}

fn tooling_profile_snippets() -> Vec<String> {
    vec![
        "if command -v starship >/dev/null 2>&1; then eval \"$(starship init bash)\"; fi"
            .to_string(),
        "if command -v zoxide >/dev/null 2>&1; then eval \"$(zoxide init bash)\"; fi".to_string(),
        "if command -v atuin >/dev/null 2>&1; then eval \"$(atuin init bash --disable-up-arrow)\"; fi"
            .to_string(),
    ]
}

fn kdl_quote(raw: &str) -> String {
    let escaped = raw
        .replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n");
    format!("\"{escaped}\"")
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerifyHalt {
    Drift(Vec<rmcp_mux::ipc::command::NonMuxEntry>),
    Timeout,
}

pub fn pre_launch_verify(client_kind: rmcp_mux::ipc::ClientKind) -> Result<(), VerifyHalt> {
    use std::io::{BufRead, BufReader, Write};
    use std::os::unix::net::UnixStream;
    use std::time::Duration;

    let path = rmcp_mux::ipc::socket_path();
    let stream = match UnixStream::connect(&path) {
        Ok(s) => s,
        Err(_) => return Ok(()),
    };

    let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
    let _ = stream.set_write_timeout(Some(Duration::from_secs(5)));

    let cmd = rmcp_mux::ipc::MuxControlCommand::Verify { client_kind };
    let Ok(json) = serde_json::to_string(&cmd) else {
        return Ok(());
    };

    let mut writer = &stream;
    if writeln!(writer, "{json}").is_err() {
        return Err(VerifyHalt::Timeout);
    }

    let mut reader = BufReader::new(stream);
    let mut response_line = String::new();
    if reader.read_line(&mut response_line).is_err() || response_line.is_empty() {
        return Err(VerifyHalt::Timeout);
    }

    if let Ok(rmcp_mux::ipc::MuxControlResponse::VerifyResult(res)) =
        serde_json::from_str(&response_line)
        && !res.ok
    {
        return Err(VerifyHalt::Drift(res.non_mux_servers));
    }
    Ok(())
}