Files
vibe-kanban/crates/executors/src/mcp_config.rs
Louis Knight-Webb 7de87e9b3a Add QA mode for automated testing with mock executor and hardcoded repos (Vibe Kanban) (#1940)
* The QA mode implementation is complete. Here's a summary of what was implemented:

## Summary

I've successfully implemented the QA mode feature for the vibe-kanban application. Here's what was created/modified:

### New Files
1. **`crates/services/src/services/qa_repos.rs`** - QA repos module that:
   - Defines two hardcoded QA repo URLs (`internal-qa-1` and `internal-qa-2`)
   - Clones repos to a persistent temp directory on first access using `git2`
   - Returns `DirectoryEntry` compatible with existing API

2. **`crates/executors/src/executors/qa_mock.rs`** - Mock executor that:
   - Implements `StandardCodingAgentExecutor` trait
   - Performs random file operations (create, delete, modify)
   - Streams 10 mock log entries in ClaudeJson format over 10 seconds
   - Reuses Claude's log processor for normalization

### Modified Files
1. **Cargo.toml files** - Added `qa-mode` feature flags to:
   - `crates/executors/Cargo.toml`
   - `crates/services/Cargo.toml`
   - `crates/server/Cargo.toml`

2. **`crates/executors/src/executors/mod.rs`** - Added:
   - Conditional `qa_mock` module export
   - `QaMock` variant to `CodingAgent` enum
   - Updated `capabilities()` match arm

3. **`crates/executors/src/mcp_config.rs`** - Added `QaMock` handling

4. **`crates/services/src/services/filesystem.rs`** - Override `list_git_repos` and `list_common_git_repos` in QA mode

5. **`crates/services/src/services/container.rs`** - Override `normalize_logs` executor lookup in QA mode

6. **`crates/executors/src/actions/coding_agent_initial.rs`** - Override spawn in QA mode

7. **`crates/executors/src/actions/coding_agent_follow_up.rs`** - Override spawn in QA mode

8. **`vibe-kanban/package.json`** - Added QA build scripts:
   - `backend:dev:qa`
   - `backend:dev:watch:qa`
   - `dev:qa`

### Usage
```bash
# Run backend in QA mode
cargo run --features qa-mode

# Or with pnpm
pnpm run dev:qa
```

When QA mode is enabled:
- `GitPanelCreate` will show only `internal-qa-1` and `internal-qa-2` as recent repos
- Starting any task will perform mock file operations and stream 10 logs over 10 seconds
- Logs follow the ClaudeJson format for compatibility with the existing log processor

* Cleanup script changes for workspace 15499061-ec87-4c1c-81b9-994e84d0e75b

* The TLS error fix is complete. I've replaced the `git2::Repository::clone` call with the `git` CLI approach in `qa_repos.rs`:

**Changes made:**
- Updated `clone_qa_repos_if_needed` function to use `Command::new("git")` with `["clone", "--depth", "1", url, path]` arguments instead of `git2::Repository::clone`
- Added `--depth 1` for shallow cloning (faster)
- Updated error handling to capture stderr output from the git command

The fix uses the git CLI which has reliable TLS support, avoiding the TLS configuration issues that git2 can have on some systems. This matches patterns used elsewhere in the codebase (e.g., `crates/remote/src/github_app/service.rs`).

You can now run `pnpm run backend:dev:qa` or `pnpm run dev:qa` and the QA repos should clone successfully.

* Cleanup script changes for workspace 15499061-ec87-4c1c-81b9-994e84d0e75b

* Done! I've made the mock logs strongly typed by:

1. **Added imports** for `ClaudeJson`, `ClaudeMessage`, `ClaudeContentItem`, and `ClaudeToolData` from the Claude executor

2. **Replaced `generate_mock_logs()`** to use strongly-typed structs:
   - Each log entry is now a proper `ClaudeJson` enum variant
   - Uses `ClaudeMessage` with proper fields (`id`, `message_type`, `role`, `model`, `content`, `stop_reason`)
   - Uses `ClaudeContentItem` variants (`Thinking`, `ToolUse`, `ToolResult`, `Text`)
   - Uses `ClaudeToolData` variants (`Read`, `Write`, `Bash`)
   - Serializes to JSON using `serde_json::to_string()` for correct escaping

3. **Updated tests** to verify the logs deserialize back to `ClaudeJson` types

This ensures:
- All fields match what `ClaudeLogProcessor::process_logs()` expects
- No manual JSON escaping needed (serde handles it)
- Compile-time type checking prevents malformed logs
- Session IDs are consistent across all entries

* Cleanup script changes for workspace 15499061-ec87-4c1c-81b9-994e84d0e75b

* Done! I've fixed the log display issue by replacing the shell echo approach with a temp file approach:

**Changes made to `crates/executors/src/executors/qa_mock.rs`:**

1. **Write logs to a temp file** instead of embedding them in a shell script:
   - Creates a temp file at `$TMPDIR/qa_mock_logs_{uuid}.jsonl`
   - Writes all logs as newline-delimited JSON

2. **Use `IFS= read -r` to read the file** - this preserves exact content:
   - `IFS=` prevents word splitting
   - `-r` prevents backslash interpretation
   - `echo "$line"` outputs the exact line content

3. **Clean up the temp file** after reading with `rm -f`

**Benefits:**
- No shell escaping needed - file contents are read verbatim
- Each line is output exactly as serialized by serde_json
- Avoids issues with single quotes, backslashes, and escape sequences
- Temp file is automatically cleaned up after use

This should fix the issue where logs were appearing as raw JSON or with corrupted content due to shell escaping issues.

* fix formatting

* Build succeeds. I've applied the same fix to the two remaining locations that had newlines in their content:

1. **Line 234** - README tool result: Changed `\n\n` to `\\n\\n`
2. **Line 302** - Bash tool result: Changed `\n` to `\\n`

This matches the pattern the user established in the assistant final message (line 318) where `\\n` is used instead of `\n` to ensure newlines are properly preserved through the serialization and display pipeline.

* Cleanup script changes for workspace 15499061-ec87-4c1c-81b9-994e84d0e75b

* simplify scripts

* update agents.md
2026-01-11 15:39:07 +00:00

310 lines
9.6 KiB
Rust

//! Utilities for reading and writing external agent config files (not the server's own config).
//!
//! These helpers abstract over JSON vs TOML formats used by different agents.
use std::{collections::HashMap, sync::LazyLock};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use tokio::fs;
use ts_rs::TS;
use crate::executors::{CodingAgent, ExecutorError};
static DEFAULT_MCP_JSON: &str = include_str!("../default_mcp.json");
pub static PRECONFIGURED_MCP_SERVERS: LazyLock<Value> = LazyLock::new(|| {
serde_json::from_str::<Value>(DEFAULT_MCP_JSON).expect("Failed to parse default MCP JSON")
});
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
pub struct McpConfig {
servers: HashMap<String, serde_json::Value>,
pub servers_path: Vec<String>,
pub template: serde_json::Value,
pub preconfigured: serde_json::Value,
pub is_toml_config: bool,
}
impl McpConfig {
pub fn new(
servers_path: Vec<String>,
template: serde_json::Value,
preconfigured: serde_json::Value,
is_toml_config: bool,
) -> Self {
Self {
servers: HashMap::new(),
servers_path,
template,
preconfigured,
is_toml_config,
}
}
pub fn set_servers(&mut self, servers: HashMap<String, serde_json::Value>) {
self.servers = servers;
}
}
/// Read an agent's external config file (JSON or TOML) and normalize it to serde_json::Value.
pub async fn read_agent_config(
config_path: &std::path::Path,
mcp_config: &McpConfig,
) -> Result<Value, ExecutorError> {
if let Ok(file_content) = fs::read_to_string(config_path).await {
if mcp_config.is_toml_config {
// Parse TOML then convert to JSON Value
if file_content.trim().is_empty() {
return Ok(serde_json::json!({}));
}
let toml_val: toml::Value = toml::from_str(&file_content)?;
let json_string = serde_json::to_string(&toml_val)?;
Ok(serde_json::from_str(&json_string)?)
} else {
Ok(serde_json::from_str(&file_content)?)
}
} else {
Ok(mcp_config.template.clone())
}
}
/// Write an agent's external config (as serde_json::Value) back to disk in the agent's format (JSON or TOML).
pub async fn write_agent_config(
config_path: &std::path::Path,
mcp_config: &McpConfig,
config: &Value,
) -> Result<(), ExecutorError> {
if mcp_config.is_toml_config {
// Convert JSON Value back to TOML
let toml_value: toml::Value = serde_json::from_str(&serde_json::to_string(config)?)?;
let toml_content = toml::to_string_pretty(&toml_value)?;
fs::write(config_path, toml_content).await?;
} else {
let json_content = serde_json::to_string_pretty(config)?;
fs::write(config_path, json_content).await?;
}
Ok(())
}
type ServerMap = Map<String, Value>;
fn is_http_server(s: &Map<String, Value>) -> bool {
matches!(s.get("type").and_then(Value::as_str), Some("http"))
}
fn is_stdio(s: &Map<String, Value>) -> bool {
!is_http_server(s) && s.get("command").is_some()
}
fn extract_meta(mut obj: ServerMap) -> (ServerMap, Option<Value>) {
let meta = obj.remove("meta");
(obj, meta)
}
fn attach_meta(mut obj: ServerMap, meta: Option<Value>) -> Value {
if let Some(m) = meta {
obj.insert("meta".to_string(), m);
}
Value::Object(obj)
}
fn ensure_header(headers: &mut Map<String, Value>, key: &str, val: &str) {
match headers.get_mut(key) {
Some(Value::String(_)) => {}
_ => {
headers.insert(key.to_string(), Value::String(val.to_string()));
}
}
}
fn transform_http_servers<F>(mut servers: ServerMap, mut f: F) -> ServerMap
where
F: FnMut(Map<String, Value>) -> Map<String, Value>,
{
for (_k, v) in servers.iter_mut() {
if let Value::Object(s) = v
&& is_http_server(s)
{
let taken = std::mem::take(s);
*s = f(taken);
}
}
servers
}
// --- Adapters ---------------------------------------------------------------
fn adapt_passthrough(servers: ServerMap, meta: Option<Value>) -> Value {
attach_meta(servers, meta)
}
fn adapt_gemini(servers: ServerMap, meta: Option<Value>) -> Value {
let servers = transform_http_servers(servers, |mut s| {
let url = s
.remove("url")
.unwrap_or_else(|| Value::String(String::new()));
let mut headers = s
.remove("headers")
.and_then(|v| v.as_object().cloned())
.unwrap_or_default();
ensure_header(
&mut headers,
"Accept",
"application/json, text/event-stream",
);
Map::from_iter([
("httpUrl".to_string(), url),
("headers".to_string(), Value::Object(headers)),
])
});
attach_meta(servers, meta)
}
fn adapt_cursor(servers: ServerMap, meta: Option<Value>) -> Value {
let servers = transform_http_servers(servers, |mut s| {
let url = s
.remove("url")
.unwrap_or_else(|| Value::String(String::new()));
let headers = s
.remove("headers")
.unwrap_or_else(|| Value::Object(Default::default()));
Map::from_iter([("url".to_string(), url), ("headers".to_string(), headers)])
});
attach_meta(servers, meta)
}
fn adapt_codex(mut servers: ServerMap, mut meta: Option<Value>) -> Value {
servers.retain(|_, v| v.as_object().map(is_stdio).unwrap_or(false));
if let Some(Value::Object(ref mut m)) = meta {
m.retain(|k, _| servers.contains_key(k));
servers.insert("meta".to_string(), Value::Object(std::mem::take(m)));
meta = None; // already attached above
}
attach_meta(servers, meta)
}
fn adapt_opencode(servers: ServerMap, meta: Option<Value>) -> Value {
let mut servers = transform_http_servers(servers, |mut s| {
let url = s
.remove("url")
.unwrap_or_else(|| Value::String(String::new()));
let mut headers = s
.remove("headers")
.and_then(|v| v.as_object().cloned())
.unwrap_or_default();
ensure_header(
&mut headers,
"Accept",
"application/json, text/event-stream",
);
Map::from_iter([
("type".to_string(), Value::String("remote".to_string())),
("url".to_string(), url),
("headers".to_string(), Value::Object(headers)),
("enabled".to_string(), Value::Bool(true)),
])
});
for (_k, v) in servers.iter_mut() {
if let Value::Object(s) = v
&& is_stdio(s)
{
let command_str = s
.remove("command")
.and_then(|v| match v {
Value::String(s) => Some(s),
_ => None,
})
.unwrap_or_default();
let mut cmd_vec: Vec<Value> = Vec::new();
if !command_str.is_empty() {
cmd_vec.push(Value::String(command_str));
}
if let Some(arr) = s.remove("args").and_then(|v| match v {
Value::Array(arr) => Some(arr),
_ => None,
}) {
for a in arr {
match a {
Value::String(s) => cmd_vec.push(Value::String(s)),
other => cmd_vec.push(other), // fall back to raw value if not string
}
}
}
let mut new_map = Map::new();
new_map.insert("type".to_string(), Value::String("local".to_string()));
new_map.insert("command".to_string(), Value::Array(cmd_vec));
new_map.insert("enabled".to_string(), Value::Bool(true));
*s = new_map;
}
}
attach_meta(servers, meta)
}
fn adapt_copilot(mut servers: ServerMap, meta: Option<Value>) -> Value {
for (_, value) in servers.iter_mut() {
if let Value::Object(s) = value
&& !s.contains_key("tools")
{
s.insert(
"tools".to_string(),
Value::Array(vec![Value::String("*".to_string())]),
);
}
}
attach_meta(servers, meta)
}
enum Adapter {
Passthrough,
Gemini,
Cursor,
Codex,
Opencode,
Copilot,
}
fn apply_adapter(adapter: Adapter, canonical: Value) -> Value {
let (servers_only, meta) = match canonical.as_object() {
Some(map) => extract_meta(map.clone()),
None => (ServerMap::new(), None),
};
match adapter {
Adapter::Passthrough => adapt_passthrough(servers_only, meta),
Adapter::Gemini => adapt_gemini(servers_only, meta),
Adapter::Cursor => adapt_cursor(servers_only, meta),
Adapter::Codex => adapt_codex(servers_only, meta),
Adapter::Opencode => adapt_opencode(servers_only, meta),
Adapter::Copilot => adapt_copilot(servers_only, meta),
}
}
impl CodingAgent {
pub fn preconfigured_mcp(&self) -> Value {
use Adapter::*;
let adapter = match self {
CodingAgent::ClaudeCode(_) | CodingAgent::Amp(_) | CodingAgent::Droid(_) => Passthrough,
CodingAgent::QwenCode(_) | CodingAgent::Gemini(_) => Gemini,
CodingAgent::CursorAgent(_) => Cursor,
CodingAgent::Codex(_) => Codex,
CodingAgent::Opencode(_) => Opencode,
CodingAgent::Copilot(..) => Copilot,
#[cfg(feature = "qa-mode")]
CodingAgent::QaMock(_) => Passthrough, // QA mock doesn't need MCP
};
let canonical = PRECONFIGURED_MCP_SERVERS.clone();
apply_adapter(adapter, canonical)
}
}