Skip to main content

rz_cli/
bootstrap.rs

1//! Bootstrap message sent to newly spawned agents.
2
3use eyre::Result;
4
5use crate::zellij;
6
7/// Build bootstrap instructions for a newly spawned agent.
8///
9/// Includes: identity, how to communicate, who else is running.
10pub fn build(pane_id: &str, name: Option<&str>, rz_path: &str) -> Result<String> {
11    let panes = zellij::list_panes()?;
12    let identity = name.unwrap_or(pane_id);
13
14    let mut peers = String::new();
15    for p in &panes {
16        if p.is_plugin || p.pane_id() == pane_id {
17            continue;
18        }
19        let cmd = p.pane_command.as_deref().unwrap_or("shell");
20        let tab = p.tab_name.as_deref().unwrap_or("-");
21        peers.push_str(&format!("  - {} ({}, tab: {})\n", p.pane_id(), cmd, tab));
22    }
23    if peers.is_empty() {
24        peers.push_str("  (none)\n");
25    }
26
27    // Check if workspace exists.
28    let workspace = std::env::var("ZELLIJ_SESSION_NAME")
29        .ok()
30        .map(|s| format!("/tmp/rz-{s}"))
31        .filter(|p| std::path::Path::new(p).exists());
32
33    let workspace_section = if let Some(ref ws) = workspace {
34        format!(
35            r#"### Workspace
36
37A shared workspace is available at `{ws}/shared/`.
38Write large outputs (research, code drafts, logs) there instead of
39inlining them in messages. Reference the file path in your message, e.g.:
40`{rz_path} send 0 "findings at {ws}/shared/research.md"`
41"#
42        )
43    } else {
44        String::new()
45    };
46
47    Ok(format!(
48        r#"## Multi-Agent Environment
49
50You are agent "{identity}" (pane: {pane_id}) in a multi-agent Zellij session.
51
52You are **long-lived** — you will receive multiple tasks over time, not just one.
53After completing a task, report back and wait for the next one. Your context
54and knowledge accumulate across tasks, making you more valuable over time.
55Do not exit after finishing a task.
56
57### Communication
58
59You have `rz` at `{rz_path}`. Use it to talk to other agents:
60
61```bash
62# Send a message to another agent (use just the number)
63{rz_path} send <pane_id> "your message"
64
65# Send and block until reply (timeout in seconds)
66{rz_path} send --wait 30 <pane_id> "question"
67
68# List all agents
69{rz_path} list
70
71# Session overview with message counts
72{rz_path} status
73
74# Read another agent's scrollback (last N lines)
75{rz_path} dump <pane_id> --last 50
76
77# View protocol messages only
78{rz_path} log <pane_id>
79
80# Broadcast to all agents
81{rz_path} broadcast "message"
82```
83
84{workspace_section}### Active agents
85
86{peers}
87### Protocol
88
89When you receive a message starting with `@@RZ:` it is a protocol envelope.
90The JSON inside has `from`, `kind`, and `ts` fields. Reply with
91`{rz_path} send --ref <message_id> <from_pane_id> "your response"`.
92
93Keep messages short. Use the workspace for large outputs."#
94    ))
95}