myco 0.1.1

Multi-host coding agent CLI (local in-process + SSH remotes)
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
//! Root-only tool: session metadata + agent-process meta (executable path).
//!
//! Installed on the in-process local worker only (not remotes). Bound to the
//! interactive process's [`ActiveSession`].

use std::sync::Arc;

use crate::core::Async;
use crate::generative_model::{self, ToolResult};
use crate::session::{
    ActiveSession, Session, SessionLink, format_link_one_line, format_session_detail,
    format_session_list_line, list_all_sessions, list_sessions, normalize_pr_url, parse_pr_fields,
};

use super::{HostDispatchContext, ToolService};

const TOOL_DESCRIPTION: &str = r#"
Read and update Myco conversation session metadata, and inspect the running agent binary.

Sessions store a title, structured links (GitHub PRs across repos, worktree paths on any
host), and a markdown scratchpad. Files live at `~/.myco/session/{shard}/{id}.json`.

Actions:
- get: metadata for the current session (default) or another session via `session_id`
  (id or unique prefix). Always includes the on-disk file path and timestamps.
- list: enumerate sessions (id, created, updated, title, link counts, path). Optional
  `limit` (default 20; 0 = all readable sessions).
- set_title: set or clear (`title` null/empty clears) the **current** session title.
- set_scratchpad: replace the **current** session scratchpad (markdown; size-capped).
- add_link: attach a GitHub PR or worktree to the **current** session (deduped).
- remove_link: drop a link from the **current** session by `index`, or by `url` /
  `host`+`path`.
- executable_path: absolute path of the running `myco` agent binary
  (`std::env::current_exe`). Use with bash (`$path --version`) to read the package
  version when deciding how to update remotes (see manual `harness-ops`).

Use this tool (not bash/editor) for session files. Titles appear in `/sessions`: as soon as
the real task is clear (usually first turn), set_title a short scannable label — replace a
weak CLI auto-title from the first user line. When the session focus shifts, update the
title; do not leave a stale first-line title for long work. When you create a worktree or
open/receive a PR, add_link it (absolute path + host for worktrees).
"#;

/// Local tool bound to the interactive process's [`ActiveSession`].
pub struct SessionMetaTool {
    active: ActiveSession,
}

impl SessionMetaTool {
    pub fn new(active: ActiveSession) -> Self {
        Self { active }
    }
}

impl ToolService for SessionMetaTool {
    fn tool_specs(&self) -> Vec<generative_model::ToolSpec> {
        vec![generative_model::ToolSpec {
            name: "session_meta".to_string(),
            description: TOOL_DESCRIPTION.to_string(),
            input_schema: schemars::schema_for!(Input).to_value(),
        }]
    }

    fn dispatch_tool_use(
        self: Arc<Self>,
        tool_use: generative_model::ToolUse,
        _ctx: HostDispatchContext,
    ) -> Async<generative_model::ToolResult> {
        Box::pin(async move {
            let input: Input = match serde_json::from_value(tool_use.input.clone()) {
                Ok(v) => v,
                Err(e) => {
                    return ToolResult::err(format!("invalid session_meta input: {e}"));
                }
            };
            match self.execute(input) {
                Ok(text) => ToolResult::text(text),
                Err(e) => ToolResult::err(e),
            }
        })
    }
}

impl SessionMetaTool {
    fn execute(&self, input: Input) -> Result<String, String> {
        let action = input.action.clone().unwrap_or(ActionKind::Get);
        match action {
            ActionKind::Get => self.action_get(input.session_id.as_deref()),
            ActionKind::List => self.action_list(input.limit),
            ActionKind::SetTitle => self.action_set_title(input.title),
            ActionKind::SetScratchpad => {
                let text = input.scratchpad.unwrap_or_default();
                self.action_set_scratchpad(text)
            }
            ActionKind::AddLink => self.action_add_link(input),
            ActionKind::RemoveLink => self.action_remove_link(input),
            ActionKind::ExecutablePath => self.action_executable_path(),
        }
    }

    fn action_get(&self, session_id: Option<&str>) -> Result<String, String> {
        let session = match session_id {
            None => self.active.snapshot(),
            Some(id) => Session::load_by_id_or_prefix(id)?,
        };
        // For current session, path/title reflect in-memory state (may be newer than disk).
        Ok(format_session_detail(&session))
    }

    fn action_list(&self, limit: Option<usize>) -> Result<String, String> {
        let limit = limit.unwrap_or(20);
        let list = if limit == 0 {
            list_all_sessions()?
        } else {
            list_sessions(limit)?
        };
        if list.is_empty() {
            return Ok("(no sessions)\n".into());
        }
        let mut out = format!("sessions: {}\n", list.len());
        for (i, entry) in list.iter().enumerate() {
            out.push_str(&format_session_list_line(i + 1, entry));
            out.push('\n');
            out.push_str(&format!("      path={}\n", entry.path.display()));
            out.push_str(&format!(
                "      created={}  updated={}\n",
                entry.created_at.to_rfc3339(),
                entry.updated_at.to_rfc3339()
            ));
        }
        Ok(out)
    }

    fn action_set_title(&self, title: Option<String>) -> Result<String, String> {
        self.active.with_mut(|session| {
            let cleared = title.as_ref().map(|t| t.trim().is_empty()).unwrap_or(true);
            if cleared {
                session.set_title(None)?;
            } else {
                session.set_title(title)?;
            }
            session.touch();
            session.save()?;
            Ok(format!(
                "title set to {}\npath={}\n",
                session
                    .title
                    .as_deref()
                    .map(|t| format!("{t:?}"))
                    .unwrap_or_else(|| "(none)".into()),
                session.json_path().display()
            ))
        })
    }

    fn action_set_scratchpad(&self, text: String) -> Result<String, String> {
        self.active.with_mut(|session| {
            session.set_scratchpad(text)?;
            session.touch();
            session.save()?;
            Ok(format!(
                "scratchpad updated ({} bytes)\npath={}\n",
                session.scratchpad.len(),
                session.json_path().display()
            ))
        })
    }

    fn action_add_link(&self, input: Input) -> Result<String, String> {
        let kind = input
            .link_kind
            .ok_or_else(|| "add_link requires link_kind (github_pr | worktree)".to_string())?;
        let link = match kind {
            LinkKind::GithubPr => {
                let url_raw = input
                    .url
                    .as_deref()
                    .ok_or_else(|| "add_link github_pr requires url".to_string())?;
                let url = normalize_pr_url(url_raw)?;
                let (repo, number) = parse_pr_fields(&url);
                SessionLink::GitHubPr {
                    url,
                    repo: input.repo.or(repo),
                    number: input.number.or(number),
                    note: input.note,
                }
            }
            LinkKind::Worktree => {
                let host = input
                    .host
                    .filter(|h| !h.trim().is_empty())
                    .ok_or_else(|| "add_link worktree requires host".to_string())?;
                let path = input
                    .path
                    .filter(|p| !p.trim().is_empty())
                    .ok_or_else(|| "add_link worktree requires path".to_string())?;
                SessionLink::Worktree {
                    host: host.trim().to_string(),
                    path: path.trim().to_string(),
                    branch: input.branch,
                    note: input.note,
                }
            }
        };

        self.active.with_mut(|session| {
            session.upsert_link(link.clone())?;
            session.touch();
            session.save()?;
            Ok(format!(
                "link upserted: {}\nlinks={}\npath={}\n",
                format_link_one_line(&link),
                session.links.len(),
                session.json_path().display()
            ))
        })
    }

    fn action_remove_link(&self, input: Input) -> Result<String, String> {
        self.active.with_mut(|session| {
            let removed = if let Some(index) = input.index {
                session.remove_link_at(index)?
            } else if input.url.is_some() || input.host.is_some() {
                session.remove_link_matching(
                    input.url.as_deref(),
                    input.host.as_deref(),
                    input.path.as_deref(),
                )?
            } else {
                return Err("remove_link requires index, or url, or host (+ optional path)".into());
            };
            session.touch();
            session.save()?;
            Ok(format!(
                "removed: {}\nlinks remaining={}\npath={}\n",
                format_link_one_line(&removed),
                session.links.len(),
                session.json_path().display()
            ))
        })
    }

    fn action_executable_path(&self) -> Result<String, String> {
        let path = std::env::current_exe().map_err(|e| format!("current_exe failed: {e}"))?;
        Ok(format!("{}\n", path.display()))
    }
}

// --- input schema ------------------------------------------------------------

#[derive(
    Clone, Debug, schemars::JsonSchema, serde::Deserialize, serde::Serialize, PartialEq, Eq,
)]
struct Input {
    /// Action to perform. Defaults to `get`.
    #[serde(default)]
    action: Option<ActionKind>,
    /// Target session id or unique prefix for `get`. Omit for the current session.
    #[serde(default)]
    session_id: Option<String>,
    /// Max sessions for `list` (default 20; 0 = all).
    #[serde(default)]
    limit: Option<usize>,
    /// New title for `set_title`. Empty/null clears.
    #[serde(default)]
    title: Option<String>,
    /// Full scratchpad markdown for `set_scratchpad`.
    #[serde(default)]
    scratchpad: Option<String>,
    /// Link type for `add_link`.
    #[serde(default)]
    link_kind: Option<LinkKind>,
    /// GitHub PR URL or `org/repo#N` for `add_link` / `remove_link`.
    #[serde(default)]
    url: Option<String>,
    /// Optional org/repo for PR links.
    #[serde(default)]
    repo: Option<String>,
    /// Optional PR number.
    #[serde(default)]
    number: Option<u32>,
    /// Host name for worktree links (`local`, `devbox`, …).
    #[serde(default)]
    host: Option<String>,
    /// Absolute worktree path on `host`.
    #[serde(default)]
    path: Option<String>,
    /// Optional branch name for worktree links.
    #[serde(default)]
    branch: Option<String>,
    /// Optional free-form note on a link.
    #[serde(default)]
    note: Option<String>,
    /// Link index (from `get`) for `remove_link`.
    #[serde(default)]
    index: Option<usize>,
}

#[derive(
    Clone, Debug, schemars::JsonSchema, serde::Deserialize, serde::Serialize, PartialEq, Eq,
)]
#[serde(rename_all = "snake_case")]
enum ActionKind {
    Get,
    List,
    SetTitle,
    SetScratchpad,
    AddLink,
    RemoveLink,
    ExecutablePath,
}

#[derive(
    Clone, Debug, schemars::JsonSchema, serde::Deserialize, serde::Serialize, PartialEq, Eq,
)]
#[serde(rename_all = "snake_case")]
enum LinkKind {
    GithubPr,
    Worktree,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::CancelToken;
    use crate::generative_model::Model;
    use crate::tool_services::{HostDispatchContext, ToolService};
    use std::sync::Arc;

    fn tool_with_session(session: Session) -> (SessionMetaTool, ActiveSession) {
        let active = ActiveSession::new(session);
        (SessionMetaTool::new(active.clone()), active)
    }

    #[tokio::test]
    async fn set_title_and_get() {
        let dir = std::env::temp_dir().join(format!(
            "myco-meta-tool-{}",
            crate::session::uuid_simple_hex(uuid::Uuid::new_v4())
        ));
        std::fs::create_dir_all(&dir).unwrap();
        // SAFETY: test-only env override.
        unsafe {
            std::env::set_var("MYCO_HOME", &dir);
        }

        let (tool, active) = tool_with_session(Session::new(Model::ClaudeHaiku45));
        let tool = Arc::new(tool);
        let result = tool
            .clone()
            .dispatch_tool_use(
                generative_model::ToolUse {
                    id: "t1".into(),
                    name: "session_meta".into(),
                    input: serde_json::json!({
                        "action": "set_title",
                        "title": "  My Feature  "
                    }),
                },
                HostDispatchContext::bare(uuid::Uuid::nil(), CancelToken::new()),
            )
            .await;
        assert!(!result.is_error, "{result:?}");
        assert_eq!(active.snapshot().title.as_deref(), Some("My Feature"));

        let got = tool
            .dispatch_tool_use(
                generative_model::ToolUse {
                    id: "t2".into(),
                    name: "session_meta".into(),
                    input: serde_json::json!({"action": "get"}),
                },
                HostDispatchContext::bare(uuid::Uuid::nil(), CancelToken::new()),
            )
            .await;
        assert!(!got.is_error, "{got:?}");
        let text = got
            .content
            .iter()
            .filter_map(|c| match c {
                generative_model::Content::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect::<String>();
        assert!(text.contains("My Feature"), "{text}");

        let _ = std::fs::remove_dir_all(&dir);
        unsafe {
            std::env::remove_var("MYCO_HOME");
        }
    }

    #[tokio::test]
    async fn executable_path_returns_absolute_path() {
        let dir = std::env::temp_dir().join(format!(
            "myco-meta-exe-{}",
            crate::session::uuid_simple_hex(uuid::Uuid::new_v4())
        ));
        std::fs::create_dir_all(&dir).unwrap();
        unsafe {
            std::env::set_var("MYCO_HOME", &dir);
        }

        let (tool, _) = tool_with_session(Session::new(Model::ClaudeHaiku45));
        let tool = Arc::new(tool);
        let got = tool
            .dispatch_tool_use(
                generative_model::ToolUse {
                    id: "t1".into(),
                    name: "session_meta".into(),
                    input: serde_json::json!({"action": "executable_path"}),
                },
                HostDispatchContext::bare(uuid::Uuid::nil(), CancelToken::new()),
            )
            .await;
        assert!(!got.is_error, "{got:?}");
        let text = got
            .content
            .iter()
            .filter_map(|c| match c {
                generative_model::Content::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect::<String>();
        let path = text.trim();
        assert!(!path.is_empty(), "{text}");
        assert!(
            std::path::Path::new(path).is_absolute(),
            "expected absolute path, got {path:?}"
        );

        let _ = std::fs::remove_dir_all(&dir);
        unsafe {
            std::env::remove_var("MYCO_HOME");
        }
    }
}