use clap as _; use dirs as _; use libmcp as _; use std::fs; use std::io::{self, BufRead, BufReader, Write}; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; use std::time::Duration; use libmcp_testkit::read_json_lines; use serde as _; use serde_json::{Value, json}; use thiserror as _; use time as _; use users as _; use uuid as _; use phone_opus_test_support::PROMPT_PREFIX; mod phone_opus_test_support { pub(super) const PROMPT_PREFIX: &str = "You are being invoked in a read-only consultation mode by another model."; } type TestResult = Result>; fn must( result: Result, context: C, ) -> TestResult { result.map_err(|error| io::Error::other(format!("{context}: {error}")).into()) } fn must_some(value: Option, context: &str) -> TestResult { value.ok_or_else(|| io::Error::other(context).into()) } fn temp_root(name: &str) -> TestResult { let root = std::env::temp_dir().join(format!( "phone_opus_{name}_{}_{}", std::process::id(), must( std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH), "current time after unix epoch", )? .as_nanos() )); must(fs::create_dir_all(&root), "create temp root")?; Ok(root) } fn binary_path() -> PathBuf { PathBuf::from(env!("CARGO_BIN_EXE_phone-opus")) } struct McpHarness { child: Child, stdin: ChildStdin, stdout: BufReader, } impl McpHarness { fn spawn(state_home: &Path, extra_env: &[(&str, &str)]) -> TestResult { let mut command = Command::new(binary_path()); let _ = command .arg("mcp") .arg("serve") .env("XDG_STATE_HOME", state_home) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::inherit()); for (key, value) in extra_env { let _ = command.env(key, value); } let mut child = must(command.spawn(), "spawn mcp host")?; let stdin = must_some(child.stdin.take(), "host stdin")?; let stdout = BufReader::new(must_some(child.stdout.take(), "host stdout")?); Ok(Self { child, stdin, stdout, }) } fn initialize(&mut self) -> TestResult { self.request(json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": { "name": "mcp-hardening-test", "version": "0" } } })) } fn notify_initialized(&mut self) -> TestResult { self.notify(json!({ "jsonrpc": "2.0", "method": "notifications/initialized", })) } fn tools_list(&mut self) -> TestResult { self.request(json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}, })) } fn call_tool(&mut self, id: u64, name: &str, arguments: Value) -> TestResult { self.request(json!({ "jsonrpc": "2.0", "id": id, "method": "tools/call", "params": { "name": name, "arguments": arguments, } })) } fn request(&mut self, message: Value) -> TestResult { let encoded = must(serde_json::to_string(&message), "request json")?; must(writeln!(self.stdin, "{encoded}"), "write request")?; must(self.stdin.flush(), "flush request")?; let mut line = String::new(); let byte_count = must(self.stdout.read_line(&mut line), "read response")?; if byte_count == 0 { return Err(io::Error::other("unexpected EOF reading response").into()); } must(serde_json::from_str(&line), "response json") } fn notify(&mut self, message: Value) -> TestResult { let encoded = must(serde_json::to_string(&message), "notify json")?; must(writeln!(self.stdin, "{encoded}"), "write notify")?; must(self.stdin.flush(), "flush notify")?; Ok(()) } } impl Drop for McpHarness { fn drop(&mut self) { let _ = self.child.kill(); let _ = self.child.wait(); } } fn assert_tool_ok(response: &Value) { assert_eq!( response["result"]["isError"].as_bool(), Some(false), "tool response unexpectedly errored: {response:#}" ); } fn assert_tool_error(response: &Value) { assert_eq!( response["result"]["isError"].as_bool(), Some(true), "tool response unexpectedly succeeded: {response:#}" ); } fn tool_content(response: &Value) -> &Value { &response["result"]["structuredContent"] } fn tool_names(response: &Value) -> Vec<&str> { response["result"]["tools"] .as_array() .into_iter() .flatten() .filter_map(|tool| tool["name"].as_str()) .collect() } fn write_fake_claude_script(path: &Path) -> TestResult { let script = r#"#!/bin/sh set -eu if [ -n "${PHONE_OPUS_TEST_PWD_FILE:-}" ]; then pwd >"$PHONE_OPUS_TEST_PWD_FILE" fi if [ -n "${PHONE_OPUS_TEST_ENV_FILE:-}" ]; then { printf 'HOME=%s\n' "${HOME:-}" printf 'XDG_CONFIG_HOME=%s\n' "${XDG_CONFIG_HOME:-}" printf 'XDG_CACHE_HOME=%s\n' "${XDG_CACHE_HOME:-}" printf 'XDG_STATE_HOME=%s\n' "${XDG_STATE_HOME:-}" } >"$PHONE_OPUS_TEST_ENV_FILE" fi if [ -n "${PHONE_OPUS_TEST_ARGS_FILE:-}" ]; then printf '%s\n' "$@" >"$PHONE_OPUS_TEST_ARGS_FILE" fi if [ -n "${PHONE_OPUS_TEST_CWD_WRITE_PROBE_FILE:-}" ]; then probe_target="${PWD}/.phone_opus_write_probe" probe_error="${PHONE_OPUS_TEST_CWD_WRITE_ERROR_FILE:-/tmp/phone-opus-write.err}" if printf probe >"$probe_target" 2>"$probe_error"; then printf 'write_succeeded\n' >"$PHONE_OPUS_TEST_CWD_WRITE_PROBE_FILE" rm -f "$probe_target" else printf 'write_failed\n' >"$PHONE_OPUS_TEST_CWD_WRITE_PROBE_FILE" fi fi if [ -n "${PHONE_OPUS_TEST_STDERR:-}" ]; then printf '%s\n' "$PHONE_OPUS_TEST_STDERR" >&2 fi if [ -n "${PHONE_OPUS_TEST_STDOUT_FILE:-}" ]; then cat "$PHONE_OPUS_TEST_STDOUT_FILE" fi exit "${PHONE_OPUS_TEST_EXIT_CODE:-0}" "#; must(fs::write(path, script), "write fake claude script")?; let mut permissions = must(fs::metadata(path), "fake claude metadata")?.permissions(); permissions.set_mode(0o755); must( fs::set_permissions(path, permissions), "chmod fake claude script", )?; Ok(()) } fn seed_caller_claude_home(home: &Path) -> TestResult { let claude_root = home.join(".claude"); must( fs::create_dir_all(claude_root.join(".claude")), "create caller .claude tree", )?; must( fs::write( claude_root.join(".credentials.json"), "{\n \"auth\": \"token\"\n}\n", ), "write caller credentials", )?; must( fs::write( claude_root.join("settings.json"), "{\n \"theme\": \"default\"\n}\n", ), "write caller settings", )?; must( fs::write( claude_root.join("settings.local.json"), "{\n \"profile\": \"local\"\n}\n", ), "write caller local settings", )?; must( fs::write( claude_root.join(".claude").join("settings.local.json"), "{\n \"sandbox\": \"read-only\"\n}\n", ), "write nested caller local settings", )?; must( fs::write( claude_root.join("CLAUDE.md"), "Global Claude instructions for phone_opus tests.\n", ), "write caller CLAUDE.md", )?; Ok(()) } #[test] fn cold_start_exposes_consult_and_ops_tools() -> TestResult { let root = temp_root("cold_start")?; let state_home = root.join("state-home"); must(fs::create_dir_all(&state_home), "create state home")?; let mut harness = McpHarness::spawn(&state_home, &[])?; let initialize = harness.initialize()?; assert_eq!( initialize["result"]["protocolVersion"].as_str(), Some("2025-11-25") ); harness.notify_initialized()?; let tools = harness.tools_list()?; let tool_names = tool_names(&tools); assert!(tool_names.contains(&"consult")); assert!(tool_names.contains(&"consult_job")); assert!(tool_names.contains(&"consult_jobs")); assert!(tool_names.contains(&"health_snapshot")); assert!(tool_names.contains(&"telemetry_snapshot")); let health = harness.call_tool(3, "health_snapshot", json!({}))?; assert_tool_ok(&health); assert_eq!(tool_content(&health)["worker_generation"].as_u64(), Some(1)); Ok(()) } #[test] fn consult_can_resume_a_prior_session_with_read_only_toolset_and_requested_working_directory() -> TestResult { let root = temp_root("consult_success")?; let state_home = root.join("state-home"); let sandbox = root.join("sandbox"); let caller_home = root.join("caller-home"); must(fs::create_dir_all(&state_home), "create state home")?; must(fs::create_dir_all(&sandbox), "create sandbox")?; must(fs::create_dir_all(&caller_home), "create caller home")?; seed_caller_claude_home(&caller_home)?; let fake_claude = root.join("claude"); let stdout_file = root.join("stdout.json"); let args_file = root.join("args.txt"); let pwd_file = root.join("pwd.txt"); let env_file = root.join("env.txt"); let cwd_probe_file = root.join("cwd-write-probe.txt"); let cwd_probe_error_file = root.join("cwd-write-probe.err"); let resumed_session = "81f218eb-568b-409b-871b-f6e86d8f666f"; write_fake_claude_script(&fake_claude)?; must( fs::write( &stdout_file, serde_json::to_string(&json!({ "type": "result", "subtype": "success", "is_error": false, "duration_ms": 1234, "duration_api_ms": 1200, "num_turns": 2, "result": "oracle", "stop_reason": "end_turn", "session_id": resumed_session, "total_cost_usd": 0.125, "usage": { "input_tokens": 10, "output_tokens": 5 }, "modelUsage": { "claude-opus-4-6": { "inputTokens": 10, "outputTokens": 5 } }, "permission_denials": [], "fast_mode_state": "off", "uuid": "uuid-123" }))?, ), "write fake stdout", )?; let claude_bin = fake_claude.display().to_string(); let stdout_path = stdout_file.display().to_string(); let args_path = args_file.display().to_string(); let pwd_path = pwd_file.display().to_string(); let env_path = env_file.display().to_string(); let cwd_probe_path = cwd_probe_file.display().to_string(); let cwd_probe_error_path = cwd_probe_error_file.display().to_string(); let caller_home_path = caller_home.display().to_string(); let env = [ ("HOME", caller_home_path.as_str()), ("PHONE_OPUS_CLAUDE_BIN", claude_bin.as_str()), ("PHONE_OPUS_TEST_STDOUT_FILE", stdout_path.as_str()), ("PHONE_OPUS_TEST_ARGS_FILE", args_path.as_str()), ("PHONE_OPUS_TEST_PWD_FILE", pwd_path.as_str()), ("PHONE_OPUS_TEST_ENV_FILE", env_path.as_str()), ( "PHONE_OPUS_TEST_CWD_WRITE_PROBE_FILE", cwd_probe_path.as_str(), ), ( "PHONE_OPUS_TEST_CWD_WRITE_ERROR_FILE", cwd_probe_error_path.as_str(), ), ]; let mut harness = McpHarness::spawn(&state_home, &env)?; let _ = harness.initialize()?; harness.notify_initialized()?; let consult = harness.call_tool( 3, "consult", json!({ "prompt": "say oracle", "cwd": sandbox.display().to_string(), "session_id": resumed_session }), )?; assert_tool_ok(&consult); assert_eq!(tool_content(&consult)["response"].as_str(), Some("oracle")); assert_eq!( tool_content(&consult)["session_mode"].as_str(), Some("resumed") ); assert_eq!( tool_content(&consult)["requested_session_id"].as_str(), Some(resumed_session) ); assert_eq!( tool_content(&consult)["prompt_prefix_injected"].as_bool(), Some(true) ); assert_eq!( tool_content(&consult)["cwd"].as_str(), Some(sandbox.display().to_string().as_str()) ); assert_eq!(tool_content(&consult)["num_turns"].as_u64(), Some(2)); assert_eq!( tool_content(&consult)["session_id"].as_str(), Some(resumed_session) ); let persisted_output_path = must_some( tool_content(&consult)["persisted_output_path"] .as_str() .map(str::to_owned), "persisted output path", )?; assert!(persisted_output_path.starts_with("/tmp/phone_opus-consults/")); let persisted_output = must( fs::read_to_string(&persisted_output_path), "read persisted consult output", )?; let persisted_output: Value = must( serde_json::from_str(&persisted_output), "parse persisted consult output", )?; assert_eq!(persisted_output["response"].as_str(), Some("oracle")); assert_eq!( persisted_output["requested_session_id"].as_str(), Some(resumed_session) ); let pwd = must(fs::read_to_string(&pwd_file), "read fake pwd file")?; assert_eq!(pwd.trim(), sandbox.display().to_string()); let args = must(fs::read_to_string(&args_file), "read fake args file")?; let lines = args.lines().collect::>(); assert!(lines.contains(&"-p")); assert!(lines.contains(&"--output-format")); assert!(lines.contains(&"json")); assert!(lines.contains(&"--strict-mcp-config")); assert!(lines.contains(&"--mcp-config")); assert!(lines.contains(&"{\"mcpServers\":{}}")); assert!(lines.contains(&"--disable-slash-commands")); assert!(lines.contains(&"--no-chrome")); assert!(lines.contains(&"--model")); assert!(lines.contains(&"claude-opus-4-6")); assert!(lines.contains(&"--effort")); assert!(lines.contains(&"max")); assert!(lines.contains(&"--tools")); assert!(lines.contains(&"Bash,Read,Grep,Glob,LS,WebFetch,WebSearch")); assert!(lines.contains(&"--dangerously-skip-permissions")); assert!(!lines.contains(&"--permission-mode")); assert!(!lines.contains(&"dontAsk")); assert!(lines.contains(&"--resume")); assert!(lines.contains(&resumed_session)); assert!(!lines.contains(&"--max-turns")); assert!(args.contains(PROMPT_PREFIX)); assert!(args.contains("The real prompt follows.")); let prefix_index = must_some(args.find(PROMPT_PREFIX), "prefixed consult prompt")?; let user_prompt_index = must_some(args.find("say oracle"), "user prompt inside args")?; assert!(prefix_index < user_prompt_index); let env_dump = must(fs::read_to_string(&env_file), "read fake env file")?; let state_root = state_home.join("phone_opus"); let claude_home = state_root.join("claude-home"); let xdg_config_home = state_root.join("xdg-config"); let xdg_cache_home = state_root.join("xdg-cache"); let xdg_state_home = state_root.join("xdg-state"); assert!(env_dump.contains(format!("HOME={}", claude_home.display()).as_str())); assert!(env_dump.contains(format!("XDG_CONFIG_HOME={}", xdg_config_home.display()).as_str())); assert!(env_dump.contains(format!("XDG_CACHE_HOME={}", xdg_cache_home.display()).as_str())); assert!(env_dump.contains(format!("XDG_STATE_HOME={}", xdg_state_home.display()).as_str())); assert_eq!( must( fs::read_to_string(claude_home.join(".claude").join(".credentials.json")), "read mirrored credentials" )?, "{\n \"auth\": \"token\"\n}\n" ); assert_eq!( must( fs::read_to_string(claude_home.join(".claude").join("settings.json")), "read mirrored settings" )?, "{\n \"theme\": \"default\"\n}\n" ); assert_eq!( must( fs::read_to_string(claude_home.join(".claude").join("settings.local.json")), "read mirrored local settings" )?, "{\n \"profile\": \"local\"\n}\n" ); assert_eq!( must( fs::read_to_string( claude_home .join(".claude") .join(".claude") .join("settings.local.json") ), "read mirrored nested local settings" )?, "{\n \"sandbox\": \"read-only\"\n}\n" ); assert_eq!( must( fs::read_to_string(claude_home.join(".claude").join("CLAUDE.md")), "read mirrored CLAUDE.md" )?, "Global Claude instructions for phone_opus tests.\n" ); let cwd_probe = must( fs::read_to_string(&cwd_probe_file), "read cwd write probe result", )?; assert_eq!(cwd_probe.trim(), "write_failed"); let telemetry = harness.call_tool(4, "telemetry_snapshot", json!({}))?; assert_tool_ok(&telemetry); let hot_methods = tool_content(&telemetry)["hot_methods"] .as_array() .cloned() .unwrap_or_default(); assert!( hot_methods .iter() .any(|value| value["method"] == "tools/call:consult") ); Ok(()) } #[test] fn consult_can_run_in_background_and_be_polled() -> TestResult { let root = temp_root("consult_background")?; let state_home = root.join("state-home"); let sandbox = root.join("sandbox"); let caller_home = root.join("caller-home"); must(fs::create_dir_all(&state_home), "create state home")?; must(fs::create_dir_all(&sandbox), "create sandbox")?; must(fs::create_dir_all(&caller_home), "create caller home")?; let fake_claude = root.join("claude"); let stdout_file = root.join("stdout.json"); let args_file = root.join("args.txt"); let pwd_file = root.join("pwd.txt"); write_fake_claude_script(&fake_claude)?; must( fs::write( &stdout_file, serde_json::to_string(&json!({ "type": "result", "subtype": "success", "is_error": false, "duration_ms": 4321, "duration_api_ms": 4200, "num_turns": 3, "result": "background oracle", "stop_reason": "end_turn", "session_id": "3fc69f58-7752-4d9d-a95d-19a217814b6a", "total_cost_usd": 0.25, "usage": { "input_tokens": 11, "output_tokens": 7 }, "modelUsage": { "claude-opus-4-6": { "inputTokens": 11, "outputTokens": 7 } }, "permission_denials": [], "fast_mode_state": "off", "uuid": "uuid-456" }))?, ), "write fake stdout", )?; let claude_bin = fake_claude.display().to_string(); let stdout_path = stdout_file.display().to_string(); let args_path = args_file.display().to_string(); let pwd_path = pwd_file.display().to_string(); let caller_home_path = caller_home.display().to_string(); let env = [ ("HOME", caller_home_path.as_str()), ("PHONE_OPUS_CLAUDE_BIN", claude_bin.as_str()), ("PHONE_OPUS_TEST_STDOUT_FILE", stdout_path.as_str()), ("PHONE_OPUS_TEST_ARGS_FILE", args_path.as_str()), ("PHONE_OPUS_TEST_PWD_FILE", pwd_path.as_str()), ]; let mut harness = McpHarness::spawn(&state_home, &env)?; let _ = harness.initialize()?; harness.notify_initialized()?; let submit = harness.call_tool( 3, "consult", json!({ "prompt": "background oracle", "cwd": sandbox.display().to_string(), "background": true }), )?; assert_tool_ok(&submit); assert_eq!(tool_content(&submit)["mode"].as_str(), Some("background")); let job_id = must_some( tool_content(&submit)["job_id"].as_str().map(str::to_owned), "background job id", )?; let _ = uuid::Uuid::parse_str(&job_id) .map_err(|error| io::Error::other(format!("job id uuid parse: {error}")))?; let mut job = Value::Null; for _ in 0..100 { job = harness.call_tool( 4, "consult_job", json!({ "job_id": job_id, "render": "json" }), )?; assert_tool_ok(&job); if tool_content(&job)["status"].as_str() == Some("succeeded") { break; } std::thread::sleep(Duration::from_millis(10)); } assert_eq!(tool_content(&job)["status"].as_str(), Some("succeeded")); assert_eq!( tool_content(&job)["result"]["response"].as_str(), Some("background oracle") ); let persisted_output_path = must_some( tool_content(&job)["result"]["persisted_output_path"] .as_str() .map(str::to_owned), "background persisted output path", )?; assert!(persisted_output_path.starts_with("/tmp/phone_opus-consults/")); let persisted_output = must( fs::read_to_string(&persisted_output_path), "read background persisted consult output", )?; let persisted_output: Value = must( serde_json::from_str(&persisted_output), "parse background persisted consult output", )?; assert_eq!( persisted_output["response"].as_str(), Some("background oracle") ); let jobs = harness.call_tool(5, "consult_jobs", json!({ "render": "json" }))?; assert_tool_ok(&jobs); assert!( tool_content(&jobs)["jobs"] .as_array() .into_iter() .flatten() .any(|value| value["job_id"] == job_id) ); let args = must(fs::read_to_string(&args_file), "read fake args file")?; assert!(args.contains(PROMPT_PREFIX)); assert!(args.contains("background oracle")); let pwd = must(fs::read_to_string(&pwd_file), "read fake pwd file")?; assert_eq!(pwd.trim(), sandbox.display().to_string()); Ok(()) } #[test] fn consult_rejects_invalid_session_handles() -> TestResult { let root = temp_root("consult_invalid_session")?; let state_home = root.join("state-home"); must(fs::create_dir_all(&state_home), "create state home")?; let mut harness = McpHarness::spawn(&state_home, &[])?; let _ = harness.initialize()?; harness.notify_initialized()?; let consult = harness.call_tool( 3, "consult", json!({ "prompt": "fail", "session_id": "not-a-uuid" }), )?; assert_tool_error(&consult); assert_eq!( tool_content(&consult)["fault"]["class"].as_str(), Some("protocol") ); assert!( tool_content(&consult)["fault"]["detail"] .as_str() .is_some_and(|value| value.contains("session_id must be a valid UUID")) ); Ok(()) } #[test] fn consult_surfaces_downstream_cli_failures() -> TestResult { let root = temp_root("consult_failure")?; let state_home = root.join("state-home"); let fake_claude = root.join("claude"); let caller_home = root.join("caller-home"); must(fs::create_dir_all(&state_home), "create state home")?; must(fs::create_dir_all(&caller_home), "create caller home")?; write_fake_claude_script(&fake_claude)?; let claude_bin = fake_claude.display().to_string(); let caller_home_path = caller_home.display().to_string(); let env = [ ("HOME", caller_home_path.as_str()), ("PHONE_OPUS_CLAUDE_BIN", claude_bin.as_str()), ("PHONE_OPUS_TEST_EXIT_CODE", "17"), ("PHONE_OPUS_TEST_STDERR", "permission denied by fake claude"), ]; let mut harness = McpHarness::spawn(&state_home, &env)?; let _ = harness.initialize()?; harness.notify_initialized()?; let consult = harness.call_tool(3, "consult", json!({ "prompt": "fail" }))?; assert_tool_error(&consult); assert_eq!( tool_content(&consult)["fault"]["class"].as_str(), Some("downstream") ); assert!( tool_content(&consult)["fault"]["detail"] .as_str() .is_some_and(|value| value.contains("permission denied by fake claude")) ); Ok(()) } #[test] fn consult_never_replays_after_worker_transport_failure() -> TestResult { let root = temp_root("consult_no_replay")?; let state_home = root.join("state-home"); let fake_claude = root.join("claude"); let caller_home = root.join("caller-home"); must(fs::create_dir_all(&state_home), "create state home")?; must(fs::create_dir_all(&caller_home), "create caller home")?; write_fake_claude_script(&fake_claude)?; let claude_bin = fake_claude.display().to_string(); let caller_home_path = caller_home.display().to_string(); let env = [ ("HOME", caller_home_path.as_str()), ("PHONE_OPUS_CLAUDE_BIN", claude_bin.as_str()), ( "PHONE_OPUS_MCP_TEST_WORKER_CRASH_ONCE_KEY", "tools/call:consult", ), ]; let mut harness = McpHarness::spawn(&state_home, &env)?; let _ = harness.initialize()?; harness.notify_initialized()?; let consult = harness.call_tool(3, "consult", json!({ "prompt": "crash once" }))?; assert_tool_error(&consult); assert_eq!( tool_content(&consult)["fault"]["class"].as_str(), Some("transport") ); assert_eq!(tool_content(&consult)["retryable"].as_bool(), Some(true)); assert_eq!(tool_content(&consult)["retried"].as_bool(), Some(false)); let telemetry = harness.call_tool(4, "telemetry_snapshot", json!({ "render": "json" }))?; assert_tool_ok(&telemetry); assert_eq!(tool_content(&telemetry)["retries"].as_u64(), Some(0)); let hot_methods = tool_content(&telemetry)["hot_methods"] .as_array() .cloned() .unwrap_or_default(); let consult_method = hot_methods .iter() .find(|value| value["method"] == "tools/call:consult") .cloned() .unwrap_or_default(); assert_eq!(consult_method["transport_faults"].as_u64(), Some(1)); let telemetry_log_path = state_home .join("phone_opus") .join("mcp") .join("telemetry.jsonl"); let telemetry_rows = read_json_lines::(&telemetry_log_path)?; assert!( telemetry_rows .iter() .any(|row| row["event"] == "tool_call" && row["tool_name"] == "consult") ); Ok(()) }