Skip to content

armgit / agtx

Public Imported from GitHub
0

πŸ„πŸΌβ€β™‚οΈ The blackboard for coding agents - agentic development environment for claude code, codex, cursor, opencode, grok and more.

fix: multi agent switching problems (#60)

Fynn FlΓΌgge36541818f8b0
diff --git a/src/skills.rs b/src/skills.rs
index 29bb705..3bdc387 100644
--- a/src/skills.rs
+++ b/src/skills.rs
@@ -36,7 +36,7 @@ pub fn agent_native_skill_dir(agent_name: &str) -> Option<(&'static str, &'stati
match agent_name {
"claude" => Some((".claude/commands", "agtx")),
"gemini" => Some((".gemini/commands", "agtx")),
- "opencode" => Some((".opencode/commands", "")),
+ "opencode" => Some((".opencode/command", "")),
"codex" => Some((".codex/skills", "")),
"cursor" => Some((".cursor/skills", "")),
"copilot" => Some((".github/agents", "agtx")),
diff --git a/src/tui/app.rs b/src/tui/app.rs
index a1f5196..0d4958e 100644
--- a/src/tui/app.rs
+++ b/src/tui/app.rs
@@ -4240,6 +4240,9 @@ impl App {
prompt_trigger,
task_content,
auto_dismiss,
+ task.worktree_path.clone(),
+ project_path.to_path_buf(),
+ plugin,
);
task.agent = planning_agent;
return Ok(false);
@@ -4408,6 +4411,9 @@ impl App {
prompt_trigger,
task_content,
auto_dismiss,
+ task.worktree_path.clone(),
+ self.state.project_path.clone().unwrap_or_default(),
+ plugin,
);
task.agent = running_agent;
}
@@ -4440,6 +4446,9 @@ impl App {
prompt_trigger,
task_content,
auto_dismiss,
+ task.worktree_path.clone(),
+ project_path.to_path_buf(),
+ plugin,
);
}
task.agent = review_agent.clone();
@@ -5241,6 +5250,9 @@ impl App {
prompt_trigger,
task_content,
auto_dismiss,
+ task.worktree_path.clone(),
+ self.state.project_path.clone().unwrap_or_default(),
+ plugin,
);
}
task.agent = review_agent;
@@ -7045,9 +7057,31 @@ fn spawn_send_to_agent(
prompt_trigger: Option<String>,
task_content: String,
auto_dismiss: Vec<crate::config::AutoDismiss>,
+ worktree_path: Option<String>,
+ project_path: std::path::PathBuf,
+ plugin: Option<WorkflowPlugin>,
) {
std::thread::spawn(move || {
if needs_switch {
+ // Deploy skills for the incoming agent only if its native skill directory
+ // doesn't exist yet. This handles the case where a worktree was created
+ // with a different agent (e.g. Claude for planning) and a new agent
+ // (e.g. OpenCode for review) is switched in later.
+ if let Some(ref wt_path) = worktree_path {
+ let already_deployed = skills::agent_native_skill_dir(&target_agent)
+ .map(|(base, namespace)| {
+ let dir = if namespace.is_empty() {
+ Path::new(wt_path).join(base)
+ } else {
+ Path::new(wt_path).join(base).join(namespace)
+ };
+ dir.exists()
+ })
+ .unwrap_or(true); // no native path for this agent β€” nothing to deploy
+ if !already_deployed {
+ write_skills_to_worktree(wt_path, &project_path, &plugin, &[&target_agent]);
+ }
+ }
let agent_ops = agent_registry.get(&target_agent);
let new_cmd = agent_ops.build_interactive_command("");
switch_agent_in_tmux(tmux_ops.as_ref(), &target, &current_agent, &new_cmd);
@@ -7080,12 +7114,13 @@ fn send_skill_and_prompt(
agent_name: &str,
auto_dismiss: &[crate::config::AutoDismiss],
) {
- // Gemini & Codex: always combine skill+prompt into a single message.
+ // Gemini, Codex & OpenCode: always combine skill+prompt into a single message.
// Gemini: sending separately causes it to execute the skill and queue the
// prompt, which gets lost or arrives too late.
// Codex: skill mentions ($skill-name) are inline references that must be
// part of a message β€” sending just "$skill" standalone does nothing.
- if matches!(agent_name, "gemini" | "codex" | "cursor") {
+ // OpenCode: same Ink TUI behavior as Gemini/Codex, needs combined send + double Enter.
+ if matches!(agent_name, "gemini" | "codex" | "cursor" | "opencode") {
let text_to_send = if let Some(cmd) = skill_cmd {
if !prompt.is_empty() {
Some(format!("{}\n\n{}", cmd, prompt))
@@ -7123,6 +7158,28 @@ fn send_skill_and_prompt(
}
std::thread::sleep(std::time::Duration::from_millis(200));
let _ = tmux_ops.send_keys_literal(target, "Enter");
+
+ // Codex and OpenCode show a command picker popup when a skill is typed.
+ // The first Enter confirms/closes the picker; a second Enter is needed
+ // to actually submit the message.
+ // - Codex: wait for "Press enter to insert" to disappear
+ // - OpenCode: wait a short delay (picker closes immediately on Enter)
+ if agent_name == "codex" {
+ for _ in 0..20 {
+ // up to 4s
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ if let Ok(content) = tmux_ops.capture_pane(target) {
+ if !content.contains("Press enter to insert") {
+ break;
+ }
+ }
+ }
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ let _ = tmux_ops.send_keys_literal(target, "Enter");
+ } else if agent_name == "opencode" {
+ std::thread::sleep(std::time::Duration::from_millis(400));
+ let _ = tmux_ops.send_keys_literal(target, "Enter");
+ }
}
return;
}
@@ -7513,18 +7570,38 @@ fn switch_agent_in_tmux(
};
if let Some(cmd) = exit_cmd {
- let _ = tmux_ops.send_keys(target, cmd);
+ // For Gemini (Ink/Node TUI): send text first, wait for it to appear in pane,
+ // then send Enter β€” same pattern as send_skill_and_prompt. Without this delay,
+ // Enter fires before the Ink TUI has rendered the input, and /quit is lost.
+ if current_agent == "gemini" {
+ let _ = tmux_ops.send_keys_literal(target, cmd);
+ for _ in 0..20 {
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ if let Ok(content) = tmux_ops.capture_pane(target) {
+ if content.contains(cmd) {
+ break;
+ }
+ }
+ }
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ let _ = tmux_ops.send_keys_literal(target, "Enter");
+ } else {
+ let _ = tmux_ops.send_keys(target, cmd);
+ }
} else {
let _ = tmux_ops.send_keys_literal(target, "C-c");
}
- // 2. Poll for shell (agent exited). If the agent was busy, the exit command
+ // 2. Poll for agent exit. If the agent was busy, the exit command
// may have been queued β€” so we wait up to 3s for it to take effect.
+ // Uses is_agent_active (checks both pane_current_command AND pane content)
+ // so that Node/Ink agents like Gemini (which always show "bash" as the
+ // process name) are correctly detected as still running.
let mut found_shell = false;
for _ in 0..30 {
// 3s
std::thread::sleep(std::time::Duration::from_millis(100));
- if is_pane_at_shell(tmux_ops, target) {
+ if !is_agent_active(tmux_ops, target) {
found_shell = true;
break;
}
@@ -7536,14 +7613,28 @@ fn switch_agent_in_tmux(
std::thread::sleep(std::time::Duration::from_millis(1000));
if let Some(cmd) = exit_cmd {
- let _ = tmux_ops.send_keys(target, cmd);
+ if current_agent == "gemini" {
+ let _ = tmux_ops.send_keys_literal(target, cmd);
+ for _ in 0..20 {
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ if let Ok(content) = tmux_ops.capture_pane(target) {
+ if content.contains(cmd) {
+ break;
+ }
+ }
+ }
+ std::thread::sleep(std::time::Duration::from_millis(200));
+ let _ = tmux_ops.send_keys_literal(target, "Enter");
+ } else {
+ let _ = tmux_ops.send_keys(target, cmd);
+ }
}
- // Wait for shell after retry
+ // Wait for agent exit after retry
for _ in 0..50 {
// 5s
std::thread::sleep(std::time::Duration::from_millis(100));
- if is_pane_at_shell(tmux_ops, target) {
+ if !is_agent_active(tmux_ops, target) {
found_shell = true;
break;
}
@@ -7556,7 +7647,7 @@ fn switch_agent_in_tmux(
for _ in 0..20 {
// 2s
std::thread::sleep(std::time::Duration::from_millis(100));
- if is_pane_at_shell(tmux_ops, target) {
+ if !is_agent_active(tmux_ops, target) {
break;
}
}
@@ -7787,7 +7878,7 @@ fn write_skills_to_worktree(
let _ = std::fs::write(skill_subdir.join("SKILL.md"), &content);
}
"opencode" => {
- // OpenCode uses flat .md command files: .opencode/commands/agtx-research.md
+ // OpenCode uses flat .md command files: .opencode/command/agtx-research.md
// Commands have description frontmatter + prompt template
let oc_content = transform_skill_for_opencode(&content);
let filename = skills::skill_dir_to_filename(skill_dir_name, agent_name);
diff --git a/src/tui/app_tests.rs b/src/tui/app_tests.rs
index 67762ac..88ca02c 100644
--- a/src/tui/app_tests.rs
+++ b/src/tui/app_tests.rs
@@ -1770,7 +1770,7 @@ fn test_agent_native_skill_dir() {
);
assert_eq!(
skills::agent_native_skill_dir("opencode"),
- Some((".opencode/commands", ""))
+ Some((".opencode/command", ""))
);
assert_eq!(
skills::agent_native_skill_dir("codex"),
@@ -2903,13 +2903,14 @@ fn test_switch_agent_gemini_sends_quit() {
let quit_sent = Arc::new(AtomicBool::new(false));
let quit_sent_c = quit_sent.clone();
- mock.expect_send_keys().returning(move |_, k| {
+ mock.expect_send_keys().returning(|_, _| Ok(()));
+ // Gemini /quit is sent via send_keys_literal (needs delay before Enter for Ink TUI)
+ mock.expect_send_keys_literal().returning(move |_, k| {
if k == "/quit" {
quit_sent_c.store(true, Ordering::SeqCst);
}
Ok(())
});
- mock.expect_send_keys_literal().returning(|_, _| Ok(()));
mock.expect_pane_current_command()
.returning(|_| Some("zsh".to_string()));
mock.expect_capture_pane().returning(|_| Ok(String::new()));
@@ -3541,7 +3542,7 @@ fn test_write_skills_to_worktree_opencode() {
write_skills_to_worktree(&wt, dir.path(), &None, &["opencode"]);
- let md_path = dir.path().join(".opencode/commands/agtx-plan.md");
+ let md_path = dir.path().join(".opencode/command/agtx-plan.md");
assert!(md_path.exists());
let content = std::fs::read_to_string(&md_path).unwrap();
assert!(
@@ -7682,6 +7683,10 @@ fn test_switch_agent_claude_sends_exit_then_new_cmd() {
mock_tmux
.expect_pane_current_command()
.returning(|_| Some("bash".to_string()));
+ // capture_pane returns empty content β†’ no agent indicators β†’ shell confirmed free
+ mock_tmux
+ .expect_capture_pane()
+ .returning(|_| Ok(String::new()));
// new agent command sent after shell found
mock_tmux
.expect_send_keys()
@@ -7711,6 +7716,9 @@ fn test_switch_agent_codex_sends_ctrl_c_not_exit() {
mock_tmux
.expect_pane_current_command()
.returning(|_| Some("bash".to_string()));
+ mock_tmux
+ .expect_capture_pane()
+ .returning(|_| Ok(String::new()));
mock_tmux
.expect_send_keys()
.withf(|_, cmd: &str| cmd == "codex --full-auto")
@@ -7744,6 +7752,10 @@ fn test_switch_agent_retries_with_ctrl_c_when_shell_not_found() {
Some("bash".to_string())
}
});
+ // capture_pane: no agent indicators in pane content
+ mock_tmux
+ .expect_capture_pane()
+ .returning(|_| Ok(String::new()));
// C-c sent on retry
mock_tmux
.expect_send_keys_literal()
@@ -8138,6 +8150,9 @@ fn test_switch_agent_cursor_sends_ctrl_c_not_exit() {
mock_tmux
.expect_pane_current_command()
.returning(|_| Some("bash".to_string()));
+ mock_tmux
+ .expect_capture_pane()
+ .returning(|_| Ok(String::new()));
mock_tmux
.expect_send_keys()
.withf(|_, cmd: &str| cmd == "agent --yolo")