use super::*;
pub(super) const ALLOWED_TOOLS: &str = "mcp__croft-collab__collab_open,mcp__croft-collab__collab_read,\
mcp__croft-collab__collab_status";
pub(super) const BUILTIN_TOOLS: &str = "Read,Grep,Glob";
pub(super) const SETTING_SOURCES: &str = "user";
pub(super) fn claude_search_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
if let Some(home) = std::env::var_os("HOME") {
let home = PathBuf::from(home);
dirs.push(home.join(".claude").join("local"));
dirs.push(home.join(".local").join("bin"));
dirs.push(home.join(".npm-global").join("bin"));
}
dirs.push(PathBuf::from("/opt/homebrew/bin"));
dirs.push(PathBuf::from("/usr/local/bin"));
dirs
}
pub(super) fn resolve_claude_in(on_path: bool, dirs: &[PathBuf]) -> Option<String> {
if on_path {
return Some(String::from("claude"));
}
crate::proc::find::first_in("claude", dirs).map(|p| p.to_string_lossy().into_owned())
}
pub(super) fn claude_program() -> String {
resolve_claude_in(
crate::lsp::manager::is_on_path("claude"),
&claude_search_dirs(),
)
.unwrap_or_else(|| String::from("claude"))
}
pub(crate) fn claude_command(cfg: &PairConfig) -> Result<Command> {
let exe = std::env::current_exe().context("resolving croft binary path")?;
let mcp_config = json!({
"mcpServers": {
"croft-collab": {
"command": exe.to_string_lossy(),
"args": [
"collab-agent",
"--workspace", cfg.workspace.to_string_lossy(),
"--name", format!("{}-reader", cfg.name),
],
}
}
});
let mut cmd = Command::new(claude_program());
cmd.arg("-p")
.args(["--input-format", "stream-json"])
.args(["--output-format", "stream-json"])
.arg("--verbose")
.arg("--include-partial-messages")
.args(["--permission-mode", "dontAsk"])
.args(["--setting-sources", SETTING_SOURCES])
.args(["--tools", BUILTIN_TOOLS])
.args(["--allowedTools", ALLOWED_TOOLS])
.arg("--strict-mcp-config")
.args(["--mcp-config", &mcp_config.to_string()])
.args(["--append-system-prompt", &system_prompt(&cfg.provider)])
.current_dir(&cfg.workspace);
if let Some(model) = &cfg.model {
cmd.args(["--model", model]);
}
Ok(cmd)
}
pub(super) fn handle_claude_event(
state: &Mutex<PairState>,
fence: &mut FenceMachine,
msg: &Value,
turn_tx: &Sender<TurnEnd>,
) {
match msg.get("type").and_then(Value::as_str) {
Some("system") if msg.get("subtype").and_then(Value::as_str) == Some("init") => {
let can = msg
.get("capabilities")
.and_then(Value::as_array)
.is_some_and(|caps| {
caps.iter()
.any(|c| c.as_str() == Some("interrupt_receipt_v1"))
});
state.lock().unwrap().can_interrupt = can;
}
Some("stream_event") => {
let delta = msg.pointer("/event/delta");
if delta.and_then(|d| d.get("type")).and_then(Value::as_str) == Some("text_delta")
&& let Some(text) = delta.and_then(|d| d.get("text")).and_then(Value::as_str)
{
for event in fence.push(text) {
apply_fence_event(state, event);
}
}
}
Some("result") => {
if !state.lock().unwrap().turn_active {
return;
}
for event in fence.finish() {
apply_fence_event(state, event);
}
let mut st = state.lock().unwrap();
let cancelled = st.cancelled;
st.turn_active = false;
st.cancelled = false;
st.discarding = false;
st.comment_only = false;
drop(st);
let _ = turn_tx.send(TurnEnd {
is_error: msg
.get("is_error")
.and_then(Value::as_bool)
.unwrap_or(false),
cancelled,
text: msg
.get("result")
.and_then(Value::as_str)
.unwrap_or_default()
.to_string(),
});
}
_ => {} }
}
#[cfg(test)]
pub(crate) const FAKE_CLAUDE: &str = r#"
import json, sys
log = open(sys.argv[1], "a")
mode = sys.argv[2]
# The real claude CLI is chatty on stderr while its MCP servers start; the
# tee must route this to OUTPUT, never into the model's comment boxes.
print("mcp server starting", file=sys.stderr)
sys.stderr.flush()
def emit(obj):
sys.stdout.write(json.dumps(obj) + "\n")
sys.stdout.flush()
def delta(text):
emit({"type": "stream_event", "event": {
"type": "content_block_delta",
"delta": {"type": "text_delta", "text": text}}})
emit({"type": "system", "subtype": "init",
"capabilities": ["interrupt_receipt_v1"]})
line = sys.stdin.readline()
if not line:
sys.exit(0)
log.write(line)
log.flush()
if mode == "hang":
# Read one turn, then never respond: turn_active stays true so the host
# reports busy and rejects a second turn.
import time
time.sleep(60)
sys.exit(0)
if mode == "notes":
delta("Reviewing.\n")
delta("<<<NOTE demo.txt:1>>>\n")
delta("second line could be tighter\n")
delta("<<<END>>>\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "ok"})
# Stay seated like the real CLI: wait for the next turn (or EOF).
sys.stdin.readline()
sys.exit(0)
if mode == "repeat":
# An edit on every turn, numbered so a test can tell which turn's edit
# reached the owner and watch the host gate decide turn by turn.
turn = 0
while line:
turn += 1
delta("<<<EDIT demo.txt:0:0-0:0>>>\n%d\n<<<END>>>\n" % turn)
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "ok"})
line = sys.stdin.readline()
log.write(line)
log.flush()
sys.exit(0)
delta("Let me fix that.\n")
delta("<<<EDIT demo.txt:0:6-0:11>")
delta(">>\n")
delta("streamed")
if mode == "cancel":
line2 = sys.stdin.readline()
log.write(line2)
log.flush()
delta(" MORE-AFTER-CANCEL")
delta("\n<<<END>>>\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "cancelled turn"})
else:
delta(" edit")
delta("\n<<<END>>>\nDone.\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "ok"})
if mode == "linger":
# The real claude CLI does NOT reliably exit on stdin EOF (MCP
# teardown lingers); the pilot must not gamble its own exit on it.
import time
time.sleep(60)
"#;