exomonad-core 0.1.0

ExoMonad core: effect system, WASM hosting, MCP server, built-in handlers, shared types
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
//! Generate Zellij KDL layouts for ExoMonad agents.
//!
//! Provides layout generation with zjstatus (Solarized Dark theme) and
//! consistent styling across all agent tabs.

use askama::Template;
use std::path::Path;

/// Parameters for generating an agent tab.
pub struct AgentTabParams<'a> {
    /// Display name for the tab (e.g., "🤖 473-refactor")
    pub tab_name: &'a str,
    /// Name for the main pane
    pub pane_name: &'a str,
    /// Command to run (already escaped for shell)
    pub command: &'a str,
    /// Working directory (absolute path)
    pub cwd: &'a Path,
    /// Shell to use (e.g., "/bin/zsh")
    pub shell: &'a str,
    /// Whether this tab should be focused
    pub focus: bool,
    /// Whether the pane should close when the command exits.
    /// True for short-lived agents, false for interactive sessions.
    pub close_on_exit: bool,
}

/// Template for an agent tab with interaction pane and status plugin.
#[derive(Template)]
#[template(path = "agent_tab.kdl.j2", escape = "none")]
struct AgentTab {
    tab_name: String,
    pane_name: String,
    command: String,
    cwd: String,
    shell: String,
    focus: bool,
    close_on_exit: bool,
}

/// Template for a single subagent layout (wraps agent tab with zjstatus).
#[derive(Template)]
#[template(path = "subagent.kdl.j2", escape = "none")]
struct SubagentLayout {
    agent_tab: String,
}

/// Template for the main layout with multiple agent tabs.
#[derive(Template)]
#[template(path = "main.kdl.j2", escape = "none")]
struct MainLayout {
    agent_tabs: Vec<String>,
}

/// Generate a complete layout for a single agent tab with zjstatus.
///
/// Returns a KDL layout string ready to write to a file and pass to
/// `zellij action new-tab --layout <path>`.
pub fn generate_agent_layout(params: &AgentTabParams) -> Result<String, askama::Error> {
    // Render the agent tab
    let tab = AgentTab {
        tab_name: params.tab_name.to_string(),
        pane_name: params.pane_name.to_string(),
        command: params.command.to_string(),
        cwd: params.cwd.display().to_string(),
        shell: params.shell.to_string(),
        focus: params.focus,
        close_on_exit: params.close_on_exit,
    }
    .render()?;

    // Wrap in subagent layout (includes zjstatus)
    SubagentLayout { agent_tab: tab }.render()
}

/// Generate a complete layout with multiple agent tabs and zjstatus.
///
/// Used for the main TL session with multiple tabs.
pub fn generate_main_layout(tabs: Vec<AgentTabParams>) -> Result<String, askama::Error> {
    let rendered_tabs: Result<Vec<_>, _> = tabs
        .iter()
        .map(|params| {
            AgentTab {
                tab_name: params.tab_name.to_string(),
                pane_name: params.pane_name.to_string(),
                command: params.command.to_string(),
                cwd: params.cwd.display().to_string(),
                shell: params.shell.to_string(),
                focus: params.focus,
                close_on_exit: params.close_on_exit,
            }
            .render()
        })
        .collect();

    MainLayout {
        agent_tabs: rendered_tabs?,
    }
    .render()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_agent_layout() {
        let params = AgentTabParams {
            tab_name: "🤖 473-test",
            pane_name: "Agent",
            command: "claude --prompt 'test'",
            cwd: Path::new("/tmp/test"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();

        // Verify key components
        assert!(layout.contains("tab name=\"🤖 473-test\""));
        assert!(layout.contains("focus=true"));
        assert!(layout.contains("zjstatus.wasm"));
        assert!(layout.contains("exomonad-plugin.wasm"));
        assert!(layout.contains("/bin/zsh"));
        assert!(layout.contains("claude --prompt 'test'"));
        assert!(layout.contains("/tmp/test"));
    }

    #[test]
    fn test_generate_main_layout() {
        let tabs = vec![
            AgentTabParams {
                tab_name: "Tab1",
                pane_name: "P1",
                command: "cmd1",
                cwd: Path::new("/tmp/1"),
                shell: "/bin/zsh",
                focus: true,
                close_on_exit: true,
            },
            AgentTabParams {
                tab_name: "Tab2",
                pane_name: "P2",
                command: "cmd2",
                cwd: Path::new("/tmp/2"),
                shell: "/bin/zsh",
                focus: false,
                close_on_exit: true,
            },
        ];

        let layout = generate_main_layout(tabs).unwrap();

        assert!(layout.contains("tab name=\"Tab1\""));
        assert!(layout.contains("tab name=\"Tab2\""));
        assert!(layout.contains("zjstatus.wasm"));
    }

    // === Edge case tests ===

    #[test]
    fn test_tab_name_with_emoji() {
        let params = AgentTabParams {
            tab_name: "🤖 473-fix",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(layout.contains("tab name=\"🤖 473-fix\""));
    }

    #[test]
    fn test_tab_name_with_special_chars() {
        let params = AgentTabParams {
            tab_name: "Tab-123_test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(layout.contains("tab name=\"Tab-123_test\""));
    }

    #[test]
    fn test_command_with_single_quotes() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "claude --prompt 'hello world'",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(layout.contains("claude --prompt 'hello world'"));
    }

    #[test]
    fn test_command_with_double_quotes() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: r#"echo "hello world""#,
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        // Command should be in the layout (may need escaping depending on template)
        assert!(layout.contains("echo"));
        assert!(layout.contains("hello world"));
    }

    #[test]
    fn test_cwd_with_spaces() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/path/with spaces/project"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(layout.contains("/path/with spaces/project"));
    }

    #[test]
    fn test_focus_false() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: false,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        // When focus=false, the tab line should not have focus=true
        // (KDL omits the attribute rather than setting it to false)
        assert!(layout.contains("tab name=\"Test\" {"));
        // But pane still has focus=true (inner pane focus, not tab focus)
        assert!(layout.contains("pane name=\"Agent\" focus=true"));
    }

    #[test]
    fn test_empty_pane_name() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        // Should still generate valid KDL with empty name
        assert!(layout.contains("pane name=\"\""));
    }

    // === Structure validation tests ===

    #[test]
    fn test_layout_has_zjstatus() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(
            layout.contains("zjstatus.wasm"),
            "Layout should include zjstatus plugin"
        );
    }

    #[test]
    fn test_layout_has_exomonad_plugin() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(
            layout.contains("exomonad-plugin.wasm"),
            "Layout should include exomonad plugin"
        );
    }

    #[test]
    fn test_layout_close_on_exit() {
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: "echo test",
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(
            layout.contains("close_on_exit true"),
            "Layout should include close_on_exit true"
        );
    }

    #[test]
    fn test_main_layout_multiple_tabs() {
        let tabs = vec![
            AgentTabParams {
                tab_name: "Tab1",
                pane_name: "P1",
                command: "cmd1",
                cwd: Path::new("/tmp/1"),
                shell: "/bin/zsh",
                focus: true,
                close_on_exit: true,
            },
            AgentTabParams {
                tab_name: "Tab2",
                pane_name: "P2",
                command: "cmd2",
                cwd: Path::new("/tmp/2"),
                shell: "/bin/zsh",
                focus: false,
                close_on_exit: true,
            },
            AgentTabParams {
                tab_name: "Tab3",
                pane_name: "P3",
                command: "cmd3",
                cwd: Path::new("/tmp/3"),
                shell: "/bin/zsh",
                focus: false,
                close_on_exit: true,
            },
        ];

        let layout = generate_main_layout(tabs).unwrap();

        // All tabs should be present
        assert!(layout.contains("tab name=\"Tab1\""));
        assert!(layout.contains("tab name=\"Tab2\""));
        assert!(layout.contains("tab name=\"Tab3\""));

        // Should have zjstatus
        assert!(layout.contains("zjstatus.wasm"));
    }

    #[test]
    fn test_different_shells() {
        for shell in ["/bin/bash", "/bin/zsh", "/usr/bin/fish"] {
            let params = AgentTabParams {
                tab_name: "Test",
                pane_name: "Agent",
                command: "echo test",
                cwd: Path::new("/tmp"),
                shell,
                focus: true,
                close_on_exit: true,
            };

            let layout = generate_agent_layout(&params).unwrap();
            assert!(
                layout.contains(shell),
                "Layout should contain shell: {}",
                shell
            );
        }
    }

    #[test]
    fn test_long_command() {
        let long_command = "claude --prompt 'This is a very long prompt that contains multiple sentences and should still be handled correctly by the layout generator without any truncation or issues'";
        let params = AgentTabParams {
            tab_name: "Test",
            pane_name: "Agent",
            command: long_command,
            cwd: Path::new("/tmp"),
            shell: "/bin/zsh",
            focus: true,
            close_on_exit: true,
        };

        let layout = generate_agent_layout(&params).unwrap();
        assert!(layout.contains("very long prompt"));
    }

    #[test]
    fn test_empty_tabs_list() {
        let tabs: Vec<AgentTabParams> = vec![];
        let layout = generate_main_layout(tabs).unwrap();
        // Should still produce valid KDL with zjstatus
        assert!(layout.contains("zjstatus.wasm"));
    }
}