Skip to main content

rz_cli/
bootstrap.rs

1//! Bootstrap message sent to newly spawned agents.
2
3use eyre::Result;
4
5/// Build bootstrap instructions for a multiplexer agent.
6pub fn build(surface_id: &str, name: Option<&str>, backend: &dyn crate::backend::Backend) -> Result<String> {
7    let identity = name.unwrap_or(surface_id);
8
9    let mut peers = String::new();
10    for p in backend.list_panes()? {
11        if p.is_plugin || p.id == surface_id {
12            continue;
13        }
14        let label = if p.title.is_empty() { &p.id } else { &p.title };
15        peers.push_str(&format!("  - {label}\n"));
16    }
17
18    build_common(identity, &peers, Some(surface_id))
19}
20
21/// Build bootstrap for a PTY agent (no multiplexer).
22pub fn build_pty(name: &str) -> Result<String> {
23    let mut peers = String::new();
24    if let Ok(agents) = crate::registry::list_all() {
25        for a in &agents {
26            if a.name != name {
27                peers.push_str(&format!("  - {}\n", a.name));
28            }
29        }
30    }
31
32    build_common(name, &peers, None)
33}
34
35fn build_common(identity: &str, peers: &str, surface_id: Option<&str>) -> Result<String> {
36    let peer_list = if peers.is_empty() { "  (none)\n" } else { peers };
37
38    let listener_instruction = if let Some(sid) = surface_id {
39        format!(
40            r#"
41## IMPORTANT: Start your NATS listener
42Run this IMMEDIATELY as your first action using a background bash command (run_in_background=true, timeout=600000):
43```
44rz listen {identity} --deliver cmux:{sid}
45```
46This lets you receive messages from other agents via NATS. Without it, you won't get any @@RZ: messages."#
47        )
48    } else {
49        String::new()
50    };
51
52    Ok(format!(
53        r#"You are agent "{identity}".
54
55Peers:
56{peer_list}
57## rz commands
58rz send <name> "msg"   — message an agent
59rz send lead "DONE: <summary>"  — report completion
60rz run --name <n> claude --dangerously-skip-permissions  — spawn agent
61rz ps                  — list agents
62rz logs <name>         — read agent output
63
64Messages from other agents arrive as @@RZ: JSON in your input.
65{listener_instruction}
66## Rules
671. Do the task. Report: rz send lead "DONE: <what you did>"
682. If stuck: rz send lead "BLOCKED: <issue>"
693. Stay running after reporting — wait for next task.
704. Only do what is asked. Do not explore unrelated code.
715. Do not read rz source code — use the commands above."#
72    ))
73}