use super::*;
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::sync::RwLock;
async fn spawn_fake_bug_monitor_github_mcp_server_with_issues(
seeded_issues: Vec<Value>,
) -> (String, tokio::task::JoinHandle<()>) {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("bind fake bug monitor github mcp listener");
let addr = listener
.local_addr()
.expect("fake bug monitor github mcp addr");
let issues = Arc::new(RwLock::new(seeded_issues));
let comments = Arc::new(RwLock::new(Vec::<Value>::new()));
let app = axum::Router::new().route(
"/",
axum::routing::post({
let issues = issues.clone();
let comments = comments.clone();
move |axum::Json(request): axum::Json<Value>| {
let issues = issues.clone();
let comments = comments.clone();
async move {
let id = request.get("id").cloned().unwrap_or(Value::Null);
let method = request
.get("method")
.and_then(Value::as_str)
.unwrap_or_default();
let result = match method {
"initialize" => json!({
"protocolVersion": "2024-11-05",
"capabilities": {},
"serverInfo": {
"name": "github",
"version": "test"
}
}),
"tools/list" => json!({
"tools": [
{
"name": "list_repository_issues",
"description": "List repository issues",
"inputSchema": {"type":"object"}
},
{
"name": "get_issue",
"description": "Get a GitHub issue",
"inputSchema": {"type":"object"}
},
{
"name": "mcp.github.create_issue",
"description": "Create a GitHub issue",
"inputSchema": {"type":"object"}
},
{
"name": "mcp.github.create_issue_comment",
"description": "Create a GitHub issue comment",
"inputSchema": {"type":"object"}
}
]
}),
"tools/call" => {
let name = request
.get("params")
.and_then(|row| row.get("name"))
.and_then(Value::as_str)
.unwrap_or_default();
let arguments = request
.get("params")
.and_then(|row| row.get("arguments"))
.cloned()
.unwrap_or(Value::Null);
match name {
"list_repository_issues" => {
let snapshot = issues.read().await.clone();
json!({ "issues": snapshot })
}
"get_issue" => {
let issue_number = arguments
.get("issue_number")
.and_then(Value::as_u64)
.unwrap_or_default();
let issue = issues
.read()
.await
.iter()
.find(|row| {
row.get("number").and_then(Value::as_u64)
== Some(issue_number)
})
.cloned()
.unwrap_or_else(|| {
json!({
"number": issue_number,
"title": "missing",
"body": "",
"state": "closed",
"html_url": format!("https://github.com/acme/platform/issues/{issue_number}")
})
});
json!({ "issue": issue })
}
"mcp.github.create_issue" => {
let mut issue_rows = issues.write().await;
let issue_number = (issue_rows.len() as u64) + 101;
let issue = json!({
"number": issue_number,
"title": arguments.get("title").and_then(Value::as_str).unwrap_or("Bug Monitor issue"),
"body": arguments.get("body").and_then(Value::as_str).unwrap_or(""),
"state": "open",
"html_url": format!("https://github.com/acme/platform/issues/{issue_number}")
});
issue_rows.push(issue.clone());
json!({ "issue": issue })
}
"mcp.github.create_issue_comment" => {
let mut comment_rows = comments.write().await;
let comment_id = format!("comment-{}", comment_rows.len() + 1);
let issue_number = arguments
.get("issue_number")
.and_then(Value::as_u64)
.unwrap_or_default();
let comment = json!({
"id": comment_id,
"html_url": format!("https://github.com/acme/platform/issues/{issue_number}#issuecomment-{}", comment_rows.len() + 1)
});
comment_rows.push(comment.clone());
json!({ "comment": comment })
}
other => json!({
"content": [
{
"type": "text",
"text": format!("unsupported tool {other}")
}
]
}),
}
}
other => json!({
"content": [
{
"type": "text",
"text": format!("unsupported method {other}")
}
]
}),
};
axum::Json(json!({
"jsonrpc": "2.0",
"id": id,
"result": result,
}))
}
}
}),
);
let server = tokio::spawn(async move {
axum::serve(listener, app)
.await
.expect("serve fake bug monitor github mcp");
});
(format!("http://{addr}"), server)
}
async fn spawn_fake_bug_monitor_github_mcp_server() -> (String, tokio::task::JoinHandle<()>) {
spawn_fake_bug_monitor_github_mcp_server_with_issues(Vec::new()).await
}
#[tokio::test]
async fn bug_monitor_runtime_creates_incident_and_draft_from_failure_event() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
require_approval_for_new_issues: true,
..Default::default()
})
.await
.expect("config");
let task = tokio::spawn(crate::run_bug_monitor(state.clone()));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
state.event_bus.publish(EngineEvent::new(
"session.error",
json!({
"sessionID": "session-123",
"runID": "run-123",
"reason": "Prompt retry failed",
"component": "swarm-orchestrator",
}),
));
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let incidents = state.list_bug_monitor_incidents(10).await;
let drafts = state.list_bug_monitor_drafts(10).await;
if incidents
.iter()
.any(|row| row.draft_id.is_some() || row.last_error.is_some())
|| !drafts.is_empty()
{
break;
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
})
.await
.expect("incident timeout");
let incidents = state.list_bug_monitor_incidents(10).await;
assert_eq!(incidents.len(), 1);
let incident = &incidents[0];
assert_eq!(incident.event_type, "session.error");
assert_eq!(incident.repo, "acme/platform");
assert_eq!(incident.workspace_root, "/tmp/acme");
assert!(incident.draft_id.is_some());
let drafts = state.list_bug_monitor_drafts(10).await;
assert_eq!(drafts.len(), 1);
assert_eq!(
drafts[0].draft_id,
incident.draft_id.clone().unwrap_or_default()
);
task.abort();
}
#[tokio::test]
async fn paused_bug_monitor_runtime_ignores_failure_events() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
paused: true,
repo: Some("acme/platform".to_string()),
..Default::default()
})
.await
.expect("config");
let task = tokio::spawn(crate::run_bug_monitor(state.clone()));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
state.event_bus.publish(EngineEvent::new(
"session.error",
json!({
"sessionID": "session-456",
"reason": "Paused reporter should ignore this",
}),
));
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
assert!(state.list_bug_monitor_incidents(10).await.is_empty());
assert!(state.list_bug_monitor_drafts(10).await.is_empty());
task.abort();
}
#[tokio::test]
async fn bug_monitor_runtime_detects_real_context_task_failures() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let task = tokio::spawn(crate::run_bug_monitor(state.clone()));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
state.event_bus.publish(EngineEvent::new(
"context.task.failed",
json!({
"runID": "ctx-run-fr-real-failure",
"taskID": "task-3",
"title": "Add levels, combat, UI, and audiovisual polish",
"error": "PROMPT_RETRY_FAILED",
"component": "swarm-agent-2",
"task": {
"id": "task-3",
"payload": {
"title": "Add levels, combat, UI, and audiovisual polish"
}
}
}),
));
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let incidents = state.list_bug_monitor_incidents(10).await;
let drafts = state.list_bug_monitor_drafts(10).await;
if incidents
.iter()
.any(|row| row.draft_id.is_some() || row.last_error.is_some())
|| !drafts.is_empty()
{
break;
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
})
.await
.expect("incident timeout");
let incidents = state.list_bug_monitor_incidents(10).await;
assert_eq!(incidents.len(), 1);
let incident = &incidents[0];
assert_eq!(incident.event_type, "context.task.failed");
assert!(
incident.title.contains("context.task.failed")
|| incident
.title
.contains("Add levels, combat, UI, and audiovisual polish"),
"unexpected incident title: {}",
incident.title
);
assert!(
incident.draft_id.is_some() || incident.last_error.is_some(),
"expected either a draft or a recorded reporter error"
);
task.abort();
}
#[tokio::test]
async fn bug_monitor_report_creates_and_dedupes_draft() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
require_approval_for_new_issues: true,
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let body = json!({
"report": {
"source": "desktop_logs",
"event": "orchestrator.run_failed",
"run_id": "run-123",
"excerpt": ["boom", "stack trace"],
}
});
let req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.expect("request");
let resp = app.clone().oneshot(req).await.expect("response");
assert_eq!(resp.status(), StatusCode::OK);
let payload: Value =
serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.expect("body"))
.expect("json");
let draft = payload.get("draft").expect("draft");
assert_eq!(
draft.get("repo").and_then(Value::as_str),
Some("acme/platform")
);
assert_eq!(
draft.get("status").and_then(Value::as_str),
Some("approval_required")
);
assert!(draft
.get("title")
.and_then(Value::as_str)
.unwrap_or_default()
.contains("orchestrator.run_failed"));
let req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.expect("request");
let resp = app.clone().oneshot(req).await.expect("response");
assert_eq!(resp.status(), StatusCode::OK);
let second_payload: Value =
serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.expect("body"))
.expect("json");
assert_eq!(
payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str),
second_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
);
let drafts = state.list_bug_monitor_drafts(10).await;
assert_eq!(drafts.len(), 1);
}
#[tokio::test]
async fn bug_monitor_report_surfaces_duplicate_failure_patterns() {
let state = test_state().await;
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let seed_req = Request::builder()
.method("POST")
.uri("/coder/runs")
.header("content-type", "application/json")
.body(Body::from(
json!({
"coder_run_id": "coder-run-failure-pattern-seed",
"workflow_mode": "issue_triage",
"repo_binding": {
"project_id": "proj-engine",
"workspace_id": "ws-tandem",
"workspace_root": "/tmp/tandem-repo",
"repo_slug": "acme/platform"
},
"github_ref": {
"kind": "issue",
"number": 301
}
})
.to_string(),
))
.expect("seed request");
let seed_resp = app.clone().oneshot(seed_req).await.expect("seed response");
assert_eq!(seed_resp.status(), StatusCode::OK);
let candidate_req = Request::builder()
.method("POST")
.uri("/coder/runs/coder-run-failure-pattern-seed/memory-candidates")
.header("content-type", "application/json")
.body(Body::from(
json!({
"kind": "failure_pattern",
"summary": "Repeated orchestrator failure",
"payload": {
"type": "failure.pattern",
"repo_slug": "acme/platform",
"fingerprint": "manual-failure-pattern",
"symptoms": ["orchestrator.run_failed"],
"canonical_markers": ["orchestrator.run_failed", "stack trace"],
"linked_issue_numbers": [301],
"recurrence_count": 5,
"linked_pr_numbers": [],
"affected_components": ["orchestrator"],
"artifact_refs": ["artifact://ctx/manual/triage.summary.json"]
}
})
.to_string(),
))
.expect("candidate request");
let candidate_resp = app
.clone()
.oneshot(candidate_req)
.await
.expect("candidate response");
assert_eq!(candidate_resp.status(), StatusCode::OK);
let second_seed_req = Request::builder()
.method("POST")
.uri("/coder/runs")
.header("content-type", "application/json")
.body(Body::from(
json!({
"coder_run_id": "coder-run-failure-pattern-seed-2",
"workflow_mode": "issue_triage",
"repo_binding": {
"project_id": "proj-engine",
"workspace_id": "ws-tandem",
"workspace_root": "/tmp/tandem-repo",
"repo_slug": "acme/platform"
},
"github_ref": {
"kind": "issue",
"number": 302
}
})
.to_string(),
))
.expect("second seed request");
let second_seed_resp = app
.clone()
.oneshot(second_seed_req)
.await
.expect("second seed response");
assert_eq!(second_seed_resp.status(), StatusCode::OK);
let second_candidate_req = Request::builder()
.method("POST")
.uri("/coder/runs/coder-run-failure-pattern-seed-2/memory-candidates")
.header("content-type", "application/json")
.body(Body::from(
json!({
"kind": "failure_pattern",
"summary": "stack trace orchestrator.run_failed desktop_logs",
"payload": {
"type": "failure.pattern",
"repo_slug": "acme/platform",
"fingerprint": "manual-failure-pattern",
"symptoms": ["desktop_logs", "orchestrator.run_failed"],
"canonical_markers": ["orchestrator.run_failed", "stack trace", "desktop_logs"],
"linked_issue_numbers": [302],
"recurrence_count": 1,
"linked_pr_numbers": [],
"affected_components": ["orchestrator"],
"artifact_refs": ["artifact://ctx/manual/triage.summary.json"]
}
})
.to_string(),
))
.expect("second candidate request");
let second_candidate_resp = app
.clone()
.oneshot(second_candidate_req)
.await
.expect("second candidate response");
assert_eq!(second_candidate_resp.status(), StatusCode::OK);
let report_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"event": "orchestrator.run_failed",
"fingerprint": "manual-failure-pattern",
"excerpt": ["stack trace"],
}
})
.to_string(),
))
.expect("report request");
let report_resp = app
.clone()
.oneshot(report_req)
.await
.expect("report response");
assert_eq!(report_resp.status(), StatusCode::OK);
let report_payload: Value = serde_json::from_slice(
&to_bytes(report_resp.into_body(), usize::MAX)
.await
.expect("report body"),
)
.expect("report json");
let duplicate_summary = report_payload
.get("duplicate_summary")
.cloned()
.unwrap_or(Value::Null);
assert_eq!(
report_payload.get("suppressed").and_then(Value::as_bool),
Some(true)
);
assert_eq!(
duplicate_summary.get("match_count").and_then(Value::as_u64),
Some(2)
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("fingerprint"))
.and_then(Value::as_str),
Some("manual-failure-pattern")
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("match_reason"))
.and_then(Value::as_str),
Some("exact_fingerprint")
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("recurrence_count"))
.and_then(Value::as_u64),
Some(5)
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("linked_issue_numbers"))
.and_then(Value::as_array)
.cloned(),
Some(vec![Value::from(301_u64)])
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("artifact_refs"))
.and_then(Value::as_array)
.cloned(),
Some(vec![Value::from(
"artifact://ctx/manual/triage.summary.json"
)])
);
assert_eq!(
duplicate_summary
.get("best_match")
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str),
duplicate_summary
.get("best_match")
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str)
);
assert!(duplicate_summary
.get("best_match")
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str)
.is_some_and(|value| value.starts_with("memcand-")));
assert_eq!(
report_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(2)
);
assert!(state.list_bug_monitor_drafts(10).await.is_empty());
}
#[tokio::test]
async fn bug_monitor_runtime_suppresses_duplicate_failure_patterns() {
let state = test_state().await;
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let seed_req = Request::builder()
.method("POST")
.uri("/coder/runs")
.header("content-type", "application/json")
.body(Body::from(
json!({
"coder_run_id": "coder-run-runtime-duplicate-seed",
"workflow_mode": "issue_triage",
"repo_binding": {
"project_id": "proj-engine",
"workspace_id": "ws-tandem",
"workspace_root": "/tmp/tandem-repo",
"repo_slug": "acme/platform"
},
"github_ref": {
"kind": "issue",
"number": 401
}
})
.to_string(),
))
.expect("seed request");
let seed_resp = app.clone().oneshot(seed_req).await.expect("seed response");
assert_eq!(seed_resp.status(), StatusCode::OK);
let candidate_req = Request::builder()
.method("POST")
.uri("/coder/runs/coder-run-runtime-duplicate-seed/memory-candidates")
.header("content-type", "application/json")
.body(Body::from(
json!({
"kind": "failure_pattern",
"summary": "Repeated orchestrator failure",
"payload": {
"type": "failure.pattern",
"repo_slug": "acme/platform",
"fingerprint": "runtime-duplicate-fingerprint",
"symptoms": ["session.error"],
"canonical_markers": ["Prompt retry failed", "swarm-orchestrator"],
"linked_issue_numbers": [401],
"recurrence_count": 5,
"linked_pr_numbers": [],
"affected_components": ["orchestrator"],
"artifact_refs": ["artifact://ctx/manual/triage.summary.json"]
}
})
.to_string(),
))
.expect("candidate request");
let candidate_resp = app
.clone()
.oneshot(candidate_req)
.await
.expect("candidate response");
assert_eq!(candidate_resp.status(), StatusCode::OK);
let task = tokio::spawn(crate::run_bug_monitor(state.clone()));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
state.event_bus.publish(EngineEvent::new(
"session.error",
json!({
"sessionID": "session-duplicate",
"runID": "run-duplicate",
"reason": "Prompt retry failed",
"component": "swarm-orchestrator",
}),
));
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let incidents = state.list_bug_monitor_incidents(10).await;
if incidents
.iter()
.any(|row| row.status.eq_ignore_ascii_case("duplicate_suppressed"))
{
break;
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
})
.await
.expect("duplicate timeout");
let incidents = state.list_bug_monitor_incidents(10).await;
assert_eq!(incidents.len(), 1);
let incident = &incidents[0];
assert_eq!(incident.status, "duplicate_suppressed");
let duplicate_summary = incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("matches"))
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
assert_eq!(duplicate_summary.len(), 1);
assert_eq!(
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("recurrence_count"))
.and_then(Value::as_u64),
Some(5)
);
assert_eq!(
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("linked_issue_numbers"))
.and_then(Value::as_array)
.cloned(),
Some(vec![Value::from(401_u64)])
);
assert_eq!(
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("artifact_refs"))
.and_then(Value::as_array)
.cloned(),
Some(vec![Value::from(
"artifact://ctx/manual/triage.summary.json"
)])
);
assert_eq!(
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str),
incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str)
);
assert!(incident
.duplicate_summary
.as_ref()
.and_then(|value| value.get("best_match"))
.and_then(|value| value.get("candidate_id"))
.and_then(Value::as_str)
.is_some_and(|value| value.starts_with("memcand-")));
let duplicate_matches = incident.duplicate_matches.clone().unwrap_or_default();
assert_eq!(duplicate_matches.len(), 1);
assert_eq!(
duplicate_matches[0].get("source").and_then(Value::as_str),
Some("coder_candidate")
);
assert!(incident.draft_id.is_none());
assert!(state.list_bug_monitor_drafts(10).await.is_empty());
task.abort();
}
#[tokio::test]
async fn bug_monitor_report_requires_repo() {
let state = test_state().await;
let app = app_router(state);
let req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"excerpt": ["something failed"]
}
})
.to_string(),
))
.expect("request");
let resp = app.oneshot(req).await.expect("response");
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let payload: Value =
serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.expect("body"))
.expect("json");
assert_eq!(
payload.get("code").and_then(Value::as_str),
Some("BUG_MONITOR_REPORT_INVALID")
);
}
#[tokio::test]
async fn bug_monitor_draft_can_be_approved_and_denied() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
require_approval_for_new_issues: true,
..Default::default()
})
.await
.expect("config");
let app = app_router(state);
let req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"excerpt": ["boom"],
}
})
.to_string(),
))
.expect("request");
let resp = app.clone().oneshot(req).await.expect("response");
let payload: Value =
serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.expect("body"))
.expect("json");
let draft_id = payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft_id")
.to_string();
let approve_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/approve"))
.header("content-type", "application/json")
.body(Body::from(json!({"reason":"looks valid"}).to_string()))
.expect("approve request");
let approve_resp = app
.clone()
.oneshot(approve_req)
.await
.expect("approve response");
assert_eq!(approve_resp.status(), StatusCode::OK);
let approve_payload: Value = serde_json::from_slice(
&to_bytes(approve_resp.into_body(), usize::MAX)
.await
.expect("approve body"),
)
.expect("approve json");
assert_eq!(
approve_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str),
Some("draft_ready")
);
assert_eq!(
approve_payload
.get("failure_pattern_memory")
.and_then(|row| row.get("stored"))
.and_then(Value::as_bool),
Some(true)
);
assert_eq!(
approve_payload
.get("failure_pattern_memory")
.and_then(|row| row.get("metadata"))
.and_then(|row| row.get("source"))
.and_then(Value::as_str),
Some("bug_monitor_approval")
);
assert_eq!(
approve_payload
.get("failure_pattern_memory")
.and_then(|row| row.get("metadata"))
.and_then(|row| row.get("recurrence_count"))
.and_then(Value::as_u64),
Some(1)
);
assert!(approve_payload
.get("failure_pattern_memory")
.and_then(|row| row.get("metadata"))
.and_then(|row| row.get("artifact_refs"))
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(Value::as_str)
.is_some_and(|path| {
path.ends_with("/artifacts/bug_monitor.approval_failure_pattern.json")
}));
assert!(approve_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("boom")));
assert_eq!(
approve_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(0)
);
assert_eq!(
approve_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(0)
);
assert!(
approve_payload.get("triage_summary_artifact").is_none()
|| approve_payload
.get("triage_summary_artifact")
.is_some_and(Value::is_null)
);
assert!(
approve_payload.get("issue_draft_artifact").is_none()
|| approve_payload
.get("issue_draft_artifact")
.is_some_and(Value::is_null)
);
let duplicate_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"fingerprint": approve_payload
.get("draft")
.and_then(|row| row.get("fingerprint"))
.and_then(Value::as_str)
.unwrap_or_default(),
"excerpt": ["boom"],
}
})
.to_string(),
))
.expect("duplicate request");
let duplicate_resp = app
.clone()
.oneshot(duplicate_req)
.await
.expect("duplicate response");
assert_eq!(duplicate_resp.status(), StatusCode::OK);
let duplicate_payload: Value = serde_json::from_slice(
&to_bytes(duplicate_resp.into_body(), usize::MAX)
.await
.expect("duplicate body"),
)
.expect("duplicate json");
assert_eq!(
duplicate_payload.get("suppressed").and_then(Value::as_bool),
Some(true)
);
assert_eq!(
duplicate_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
let deny_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/deny"))
.header("content-type", "application/json")
.body(Body::from(json!({"reason":"too late"}).to_string()))
.expect("deny request");
let deny_resp = app.clone().oneshot(deny_req).await.expect("deny response");
assert_eq!(deny_resp.status(), StatusCode::CONFLICT);
let second_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "billing worker checksum mismatch 0xdeadbeef",
"detail": "billing worker checksum mismatch 0xdeadbeef while loading monthly ledger",
"excerpt": ["billing-worker", "checksum mismatch 0xdeadbeef", "monthly ledger"],
"fingerprint": "manual-second"
}
})
.to_string(),
))
.expect("request");
let second_resp = app.clone().oneshot(second_req).await.expect("response");
let second_payload: Value = serde_json::from_slice(
&to_bytes(second_resp.into_body(), usize::MAX)
.await
.expect("second body"),
)
.expect("second json");
let second_draft_id = second_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("second draft id");
let deny_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{second_draft_id}/deny"))
.header("content-type", "application/json")
.body(Body::from(json!({"reason":"noise"}).to_string()))
.expect("deny request");
let deny_resp = app.clone().oneshot(deny_req).await.expect("deny response");
assert_eq!(deny_resp.status(), StatusCode::OK);
let deny_payload: Value = serde_json::from_slice(
&to_bytes(deny_resp.into_body(), usize::MAX)
.await
.expect("deny body"),
)
.expect("deny json");
assert_eq!(
deny_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str),
Some("denied")
);
}
#[tokio::test]
async fn bug_monitor_issue_draft_renders_repo_template() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let draft_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/issue-draft"))
.body(Body::empty())
.expect("issue draft request");
let draft_resp = app
.clone()
.oneshot(draft_req)
.await
.expect("issue draft response");
assert_eq!(draft_resp.status(), StatusCode::OK);
let issue_draft_payload: Value = serde_json::from_slice(
&to_bytes(draft_resp.into_body(), usize::MAX)
.await
.expect("issue draft body"),
)
.expect("issue draft json");
let rendered_body = issue_draft_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.unwrap_or_default();
assert_eq!(
issue_draft_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str),
Some(draft_id.as_str())
);
assert_eq!(
issue_draft_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
Some("Bug Monitor issue")
);
assert_eq!(
issue_draft_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_triage_summary")
);
assert!(issue_draft_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/bug_monitor.triage_summary.json")));
assert_eq!(
issue_draft_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(issue_draft_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/bug_monitor.issue_draft.json")));
assert!(rendered_body.contains("## What happened?"));
assert!(rendered_body.contains("## What did you expect to happen?"));
assert!(rendered_body.contains("## Steps to reproduce"));
assert!(rendered_body.contains("## Environment"));
assert!(rendered_body.contains("## Logs / screenshots"));
assert!(rendered_body.contains("<!-- tandem:fingerprint:v1:"));
}
#[tokio::test]
async fn bug_monitor_publish_and_recheck_fail_with_issue_draft_context() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
assert_eq!(publish_resp.status(), StatusCode::BAD_REQUEST);
let publish_payload: Value = serde_json::from_slice(
&to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body"),
)
.expect("publish json");
assert_eq!(
publish_payload.get("code").and_then(Value::as_str),
Some("BUG_MONITOR_DRAFT_PUBLISH_FAILED")
);
assert_eq!(
publish_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str),
Some(draft_id.as_str())
);
assert!(publish_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert!(matches!(
publish_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
None | Some("Bug Monitor issue")
));
assert!(matches!(
publish_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
None | Some("bug_monitor_triage_summary")
));
assert_eq!(
publish_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
let recheck_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/recheck-match"))
.body(Body::empty())
.expect("recheck request");
let recheck_resp = app
.clone()
.oneshot(recheck_req)
.await
.expect("recheck response");
assert_eq!(recheck_resp.status(), StatusCode::BAD_REQUEST);
let recheck_payload: Value = serde_json::from_slice(
&to_bytes(recheck_resp.into_body(), usize::MAX)
.await
.expect("recheck body"),
)
.expect("recheck json");
assert_eq!(
recheck_payload.get("code").and_then(Value::as_str),
Some("BUG_MONITOR_DRAFT_RECHECK_FAILED")
);
assert_eq!(
recheck_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str),
Some(draft_id.as_str())
);
assert!(recheck_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert!(matches!(
recheck_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
None | Some("Bug Monitor issue")
));
assert!(matches!(
recheck_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
None | Some("bug_monitor_triage_summary")
));
assert_eq!(
recheck_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
}
#[tokio::test]
async fn bug_monitor_publish_and_recheck_succeed_with_triage_context() {
let (endpoint, server) = spawn_fake_bug_monitor_github_mcp_server().await;
let state = test_state().await;
state
.mcp
.add_or_update(
"github".to_string(),
endpoint,
std::collections::HashMap::new(),
true,
)
.await;
assert!(state.mcp.connect("github").await);
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
mcp_server: Some("github".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
let publish_status = publish_resp.status();
let publish_body = to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body");
if publish_status != StatusCode::OK {
panic!("{}", String::from_utf8_lossy(&publish_body));
}
let publish_payload: Value = serde_json::from_slice(&publish_body).expect("publish json");
assert_eq!(
publish_payload.get("ok").and_then(Value::as_bool),
Some(true)
);
assert_eq!(
publish_payload.get("action").and_then(Value::as_str),
Some("create_issue")
);
assert_eq!(
publish_payload
.get("post")
.and_then(|row| row.get("operation"))
.and_then(Value::as_str),
Some("create_issue")
);
assert_eq!(
publish_payload
.get("external_action")
.and_then(|row| row.get("operation"))
.and_then(Value::as_str),
Some("create_issue")
);
assert_eq!(
publish_payload
.get("external_action")
.and_then(|row| row.get("source_kind"))
.and_then(Value::as_str),
Some("bug_monitor")
);
assert!(matches!(
publish_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
None | Some("Bug Monitor issue")
));
assert!(matches!(
publish_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
None | Some("bug_monitor_triage_summary")
));
assert!(publish_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert_eq!(
publish_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(matches!(
publish_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
None | Some(0)
));
let recheck_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/recheck-match"))
.body(Body::empty())
.expect("recheck request");
let recheck_resp = app
.clone()
.oneshot(recheck_req)
.await
.expect("recheck response");
let recheck_status = recheck_resp.status();
let recheck_body = to_bytes(recheck_resp.into_body(), usize::MAX)
.await
.expect("recheck body");
if recheck_status != StatusCode::OK {
panic!("{}", String::from_utf8_lossy(&recheck_body));
}
let recheck_payload: Value = serde_json::from_slice(&recheck_body).expect("recheck json");
assert_eq!(
recheck_payload.get("ok").and_then(Value::as_bool),
Some(true)
);
assert_eq!(
recheck_payload.get("action").and_then(Value::as_str),
Some("matched_open")
);
assert!(recheck_payload.get("post").is_some_and(Value::is_null));
assert!(matches!(
recheck_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
None | Some("Bug Monitor issue")
));
assert!(matches!(
recheck_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
None | Some("bug_monitor_triage_summary")
));
assert!(recheck_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert_eq!(
recheck_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(matches!(
recheck_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
None | Some(0)
));
server.abort();
}
#[tokio::test]
async fn bug_monitor_publish_comments_on_matched_open_issue_and_lists_post() {
let (endpoint, server) = spawn_fake_bug_monitor_github_mcp_server_with_issues(vec![json!({
"number": 42,
"title": "Build failure in CI",
"body": "existing issue body\n<!-- tandem:fingerprint:v1:fingerprint-match-open -->",
"state": "open",
"html_url": "https://github.com/acme/platform/issues/42"
})])
.await;
let state = test_state().await;
state
.mcp
.add_or_update(
"github".to_string(),
endpoint,
std::collections::HashMap::new(),
true,
)
.await;
assert!(state.mcp.connect("github").await);
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
mcp_server: Some("github".to_string()),
auto_comment_on_matched_open_issues: true,
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"fingerprint": "fingerprint-match-open",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
let publish_status = publish_resp.status();
let publish_body = to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body");
if publish_status != StatusCode::OK {
panic!("{}", String::from_utf8_lossy(&publish_body));
}
let publish_payload: Value = serde_json::from_slice(&publish_body).expect("publish json");
assert_eq!(
publish_payload.get("action").and_then(Value::as_str),
Some("comment_issue")
);
assert_eq!(
publish_payload
.get("post")
.and_then(|row| row.get("operation"))
.and_then(Value::as_str),
Some("comment_issue")
);
assert_eq!(
publish_payload
.get("external_action")
.and_then(|row| row.get("capability_id"))
.and_then(Value::as_str),
Some("github.comment_on_issue")
);
assert_eq!(
publish_payload
.get("post")
.and_then(|row| row.get("issue_number"))
.and_then(Value::as_u64),
Some(42)
);
assert_eq!(
publish_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str),
Some("github_comment_posted")
);
let posts_req = Request::builder()
.method("GET")
.uri("/bug-monitor/posts?limit=10")
.body(Body::empty())
.expect("posts request");
let posts_resp = app
.clone()
.oneshot(posts_req)
.await
.expect("posts response");
assert_eq!(posts_resp.status(), StatusCode::OK);
let posts_payload: Value = serde_json::from_slice(
&to_bytes(posts_resp.into_body(), usize::MAX)
.await
.expect("posts body"),
)
.expect("posts json");
assert_eq!(posts_payload.get("count").and_then(Value::as_u64), Some(1));
assert_eq!(
posts_payload
.get("posts")
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str),
Some(draft_id.as_str())
);
assert_eq!(
posts_payload
.get("posts")
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(|row| row.get("operation"))
.and_then(Value::as_str),
Some("comment_issue")
);
assert_eq!(
posts_payload
.get("posts")
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(|row| row.get("issue_number"))
.and_then(Value::as_u64),
Some(42)
);
server.abort();
}
#[tokio::test]
async fn bug_monitor_publish_reuses_existing_post_on_duplicate_submit() {
let (endpoint, server) = spawn_fake_bug_monitor_github_mcp_server().await;
let state = test_state().await;
state
.mcp
.add_or_update(
"github".to_string(),
endpoint,
std::collections::HashMap::new(),
true,
)
.await;
assert!(state.mcp.connect("github").await);
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
mcp_server: Some("github".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
let publish_status = publish_resp.status();
let publish_body = to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body");
if publish_status != StatusCode::OK {
panic!("{}", String::from_utf8_lossy(&publish_body));
}
let publish_payload: Value = serde_json::from_slice(&publish_body).expect("publish json");
assert_eq!(
publish_payload.get("action").and_then(Value::as_str),
Some("create_issue")
);
let first_post_id = publish_payload
.get("post")
.and_then(|row| row.get("post_id"))
.and_then(Value::as_str)
.expect("first post id")
.to_string();
let second_publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("second publish request");
let second_publish_resp = app
.clone()
.oneshot(second_publish_req)
.await
.expect("second publish response");
assert_eq!(second_publish_resp.status(), StatusCode::OK);
let second_publish_payload: Value = serde_json::from_slice(
&to_bytes(second_publish_resp.into_body(), usize::MAX)
.await
.expect("second publish body"),
)
.expect("second publish json");
assert_eq!(
second_publish_payload.get("action").and_then(Value::as_str),
Some("skip_duplicate")
);
assert_eq!(
second_publish_payload
.get("post")
.and_then(|row| row.get("post_id"))
.and_then(Value::as_str),
Some(first_post_id.as_str())
);
assert_eq!(
second_publish_payload
.get("draft")
.and_then(|row| row.get("github_status"))
.and_then(Value::as_str),
Some("duplicate_skipped")
);
let actions_req = Request::builder()
.method("GET")
.uri("/external-actions?limit=10")
.body(Body::empty())
.expect("actions request");
let actions_resp = app
.clone()
.oneshot(actions_req)
.await
.expect("actions response");
assert_eq!(actions_resp.status(), StatusCode::OK);
let actions_payload: Value = serde_json::from_slice(
&to_bytes(actions_resp.into_body(), usize::MAX)
.await
.expect("actions body"),
)
.expect("actions json");
assert_eq!(
actions_payload.get("count").and_then(Value::as_u64),
Some(1)
);
assert_eq!(
actions_payload
.get("actions")
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(|row| row.get("action_id"))
.and_then(Value::as_str),
Some(first_post_id.as_str())
);
assert_eq!(
actions_payload
.get("actions")
.and_then(Value::as_array)
.and_then(|rows| rows.first())
.and_then(|row| row.get("capability_id"))
.and_then(Value::as_str),
Some("github.create_issue")
);
let posts_req = Request::builder()
.method("GET")
.uri("/bug-monitor/posts?limit=10")
.body(Body::empty())
.expect("posts request");
let posts_resp = app
.clone()
.oneshot(posts_req)
.await
.expect("posts response");
assert_eq!(posts_resp.status(), StatusCode::OK);
let posts_payload: Value = serde_json::from_slice(
&to_bytes(posts_resp.into_body(), usize::MAX)
.await
.expect("posts body"),
)
.expect("posts json");
assert_eq!(posts_payload.get("count").and_then(Value::as_u64), Some(1));
server.abort();
}
#[tokio::test]
async fn bug_monitor_publish_skips_comment_when_matched_open_commenting_disabled() {
let (endpoint, server) = spawn_fake_bug_monitor_github_mcp_server_with_issues(vec![json!({
"number": 42,
"title": "Build failure in CI",
"body": "existing issue body\n<!-- tandem:fingerprint:v1:fingerprint-match-open-no-comment -->",
"state": "open",
"html_url": "https://github.com/acme/platform/issues/42"
})])
.await;
let state = test_state().await;
state
.mcp
.add_or_update(
"github".to_string(),
endpoint,
std::collections::HashMap::new(),
true,
)
.await;
assert!(state.mcp.connect("github").await);
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
mcp_server: Some("github".to_string()),
auto_comment_on_matched_open_issues: false,
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"fingerprint": "fingerprint-match-open-no-comment",
"detail": "event: orchestrator.run_failed\nprocess: tandem-engine\ncomponent: orchestrator",
"excerpt": ["boom", "stack trace"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
assert_eq!(publish_resp.status(), StatusCode::OK);
let publish_payload: Value = serde_json::from_slice(
&to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body"),
)
.expect("publish json");
assert_eq!(
publish_payload.get("action").and_then(Value::as_str),
Some("matched_open_no_comment")
);
assert!(publish_payload.get("post").is_some_and(Value::is_null));
assert_eq!(
publish_payload
.get("draft")
.and_then(|row| row.get("github_status"))
.and_then(Value::as_str),
Some("draft_ready")
);
assert_eq!(
publish_payload
.get("draft")
.and_then(|row| row.get("matched_issue_number"))
.and_then(Value::as_u64),
Some(42)
);
let posts_req = Request::builder()
.method("GET")
.uri("/bug-monitor/posts?limit=10")
.body(Body::empty())
.expect("posts request");
let posts_resp = app
.clone()
.oneshot(posts_req)
.await
.expect("posts response");
assert_eq!(posts_resp.status(), StatusCode::OK);
let posts_payload: Value = serde_json::from_slice(
&to_bytes(posts_resp.into_body(), usize::MAX)
.await
.expect("posts body"),
)
.expect("posts json");
assert_eq!(posts_payload.get("count").and_then(Value::as_u64), Some(0));
server.abort();
}
#[tokio::test]
async fn bug_monitor_issue_draft_prefers_structured_triage_summary() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Noisy raw detail",
"detail": "raw detail should not win",
"excerpt": ["noisy line"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let seeded_draft = state
.get_bug_monitor_draft(&draft_id)
.await
.expect("seeded draft");
let mut enriched_draft = seeded_draft.clone();
enriched_draft.issue_number = Some(1234);
enriched_draft.matched_issue_number = Some(1234);
state
.put_bug_monitor_draft(enriched_draft.clone())
.await
.expect("update draft");
state
.put_bug_monitor_incident(crate::BugMonitorIncidentRecord {
incident_id: "incident-structured-triage".to_string(),
fingerprint: enriched_draft.fingerprint.clone(),
event_type: "orchestrator.run_failed".to_string(),
status: "queued".to_string(),
repo: enriched_draft.repo.clone(),
workspace_root: "/tmp/acme".to_string(),
title: "Structured triage title".to_string(),
detail: Some("structured incident detail".to_string()),
excerpt: vec!["structured log line".to_string()],
source: Some("desktop_logs".to_string()),
run_id: None,
session_id: None,
correlation_id: None,
component: Some("orchestrator".to_string()),
level: Some("error".to_string()),
occurrence_count: 3,
created_at_ms: crate::now_ms(),
updated_at_ms: crate::now_ms(),
last_seen_at_ms: Some(crate::now_ms()),
draft_id: Some(draft_id.clone()),
triage_run_id: enriched_draft.triage_run_id.clone(),
last_error: None,
duplicate_summary: None,
duplicate_matches: None,
event_payload: None,
})
.await
.expect("seed incident");
let summary_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-summary"))
.header("content-type", "application/json")
.body(Body::from(
json!({
"suggested_title": "Structured triage title",
"what_happened": "Structured triage summary",
"expected_behavior": "The run should complete successfully.",
"steps_to_reproduce": [
"Open the repo",
"Run the failing workflow",
"Observe the orchestration error"
],
"environment": [
"Repo: acme/platform",
"Process: tandem-engine"
],
"logs": [
"structured log line",
"second structured line"
]
})
.to_string(),
))
.expect("summary request");
let summary_resp = app
.clone()
.oneshot(summary_req)
.await
.expect("summary response");
assert_eq!(summary_resp.status(), StatusCode::OK);
let summary_payload: Value = serde_json::from_slice(
&to_bytes(summary_resp.into_body(), usize::MAX)
.await
.expect("summary body"),
)
.expect("summary json");
let issue_draft = summary_payload.get("issue_draft").expect("issue draft");
assert_eq!(
summary_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_triage_summary")
);
assert!(summary_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/bug_monitor.triage_summary.json")));
assert_eq!(
summary_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(summary_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/bug_monitor.issue_draft.json")));
let rendered_body = issue_draft
.get("rendered_body")
.and_then(Value::as_str)
.unwrap_or_default();
assert!(rendered_body.contains("Structured triage summary"));
assert!(rendered_body.contains("The run should complete successfully."));
assert!(rendered_body.contains("1. Open the repo"));
assert!(rendered_body.contains("structured log line"));
assert!(!rendered_body.contains("raw detail should not win"));
assert_eq!(
issue_draft.get("suggested_title").and_then(Value::as_str),
Some("Structured triage title")
);
let failure_pattern_payload = summary_payload
.get("failure_pattern_memory")
.cloned()
.expect("failure pattern memory");
assert_eq!(
failure_pattern_payload
.get("stored")
.and_then(Value::as_bool),
Some(true)
);
assert_eq!(
failure_pattern_payload
.get("metadata")
.and_then(|row| row.get("kind"))
.and_then(Value::as_str),
Some("failure_pattern")
);
assert_eq!(
failure_pattern_payload
.get("metadata")
.and_then(|row| row.get("repo_slug"))
.and_then(Value::as_str),
Some("acme/platform")
);
assert_eq!(
failure_pattern_payload
.get("metadata")
.and_then(|row| row.get("recurrence_count"))
.and_then(Value::as_u64),
Some(3)
);
let regression_signal_payload = summary_payload
.get("regression_signal_memory")
.cloned()
.expect("regression signal memory");
assert_eq!(
regression_signal_payload
.get("stored")
.and_then(Value::as_bool),
Some(true)
);
assert_eq!(
regression_signal_payload
.get("metadata")
.and_then(|row| row.get("kind"))
.and_then(Value::as_str),
Some("regression_signal")
);
assert_eq!(
regression_signal_payload
.get("metadata")
.and_then(|row| row.get("repo_slug"))
.and_then(Value::as_str),
Some("acme/platform")
);
assert_eq!(
regression_signal_payload
.get("metadata")
.and_then(|row| row.get("expected_behavior"))
.and_then(Value::as_str),
Some("The run should complete successfully.")
);
let triage_run_id = summary_payload
.get("draft")
.and_then(|row| row.get("triage_run_id"))
.and_then(Value::as_str)
.expect("triage run id");
let regression_signal_artifact = load_context_blackboard(&state, triage_run_id)
.artifacts
.into_iter()
.find(|artifact| artifact.artifact_type == "bug_monitor_regression_signal_memory")
.expect("regression signal artifact");
assert!(regression_signal_artifact
.path
.ends_with("/artifacts/bug_monitor.regression_signal_memory.json"));
let duplicate_report_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Structured triage summary",
"detail": "Structured triage summary",
"excerpt": ["structured log line"],
}
})
.to_string(),
))
.expect("duplicate report request");
let duplicate_report_resp = app
.clone()
.oneshot(duplicate_report_req)
.await
.expect("duplicate report response");
assert_eq!(duplicate_report_resp.status(), StatusCode::OK);
let duplicate_report_payload: Value = serde_json::from_slice(
&to_bytes(duplicate_report_resp.into_body(), usize::MAX)
.await
.expect("duplicate report body"),
)
.expect("duplicate report json");
assert_eq!(
duplicate_report_payload
.get("suppressed")
.and_then(Value::as_bool),
Some(true)
);
}
#[tokio::test]
async fn bug_monitor_triage_run_created_from_approved_draft() {
let state = test_state().await;
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"excerpt": ["boom"],
}
})
.to_string(),
))
.expect("request");
let create_resp = app.clone().oneshot(create_req).await.expect("response");
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let draft_status = create_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
if draft_status.eq_ignore_ascii_case("approval_required") {
let approve_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/approve"))
.header("content-type", "application/json")
.body(Body::from("{}"))
.expect("approve request");
let approve_resp = app
.clone()
.oneshot(approve_req)
.await
.expect("approve response");
assert_eq!(approve_resp.status(), StatusCode::OK);
}
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let triage_payload: Value = serde_json::from_slice(
&to_bytes(triage_resp.into_body(), usize::MAX)
.await
.expect("triage body"),
)
.expect("triage json");
let run_id = triage_payload
.get("run")
.and_then(|row| row.get("run_id"))
.and_then(Value::as_str)
.expect("run id")
.to_string();
assert_eq!(
triage_payload
.get("draft")
.and_then(|row| row.get("triage_run_id"))
.and_then(Value::as_str),
Some(run_id.as_str())
);
assert_eq!(
triage_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str),
Some("triage_queued")
);
assert_eq!(
triage_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
Some("Bug Monitor issue")
);
assert_eq!(
triage_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_triage_summary")
);
assert!(triage_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert_eq!(
triage_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(triage_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/bug_monitor.issue_draft.json")));
let get_run_req = Request::builder()
.method("GET")
.uri(format!("/context/runs/{run_id}"))
.body(Body::empty())
.expect("get run request");
let get_run_resp = app
.clone()
.oneshot(get_run_req)
.await
.expect("get run response");
assert_eq!(get_run_resp.status(), StatusCode::OK);
let get_run_payload: Value = serde_json::from_slice(
&to_bytes(get_run_resp.into_body(), usize::MAX)
.await
.expect("get run body"),
)
.expect("get run json");
assert_eq!(
get_run_payload
.get("run")
.and_then(|row| row.get("run_type"))
.and_then(Value::as_str),
Some("bug_monitor_triage")
);
assert_eq!(
get_run_payload
.get("run")
.and_then(|row| row.get("tasks"))
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(2)
);
let duplicate_artifact_present = get_run_payload
.get("run")
.and_then(|row| row.get("artifacts"))
.and_then(Value::as_array)
.map(|rows| {
rows.iter().any(|row| {
row.get("artifact_type").and_then(Value::as_str)
== Some("failure_duplicate_matches")
})
})
.unwrap_or(false);
assert!(!duplicate_artifact_present);
let second_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("second triage request");
let second_resp = app
.clone()
.oneshot(second_req)
.await
.expect("second triage response");
assert_eq!(second_resp.status(), StatusCode::OK);
let second_payload: Value = serde_json::from_slice(
&to_bytes(second_resp.into_body(), usize::MAX)
.await
.expect("second triage body"),
)
.expect("second triage json");
assert_eq!(
second_payload
.get("run")
.and_then(|row| row.get("run_id"))
.and_then(Value::as_str),
Some(run_id.as_str())
);
assert_eq!(
second_payload.get("deduped").and_then(Value::as_bool),
Some(true)
);
assert_eq!(
second_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert!(
second_payload.get("duplicate_matches_artifact").is_none()
|| second_payload
.get("duplicate_matches_artifact")
.is_some_and(Value::is_null)
);
state
.put_bug_monitor_incident(crate::BugMonitorIncidentRecord {
incident_id: "incident-replay-approved-draft".to_string(),
fingerprint: triage_payload
.get("draft")
.and_then(|row| row.get("fingerprint"))
.and_then(Value::as_str)
.expect("draft fingerprint")
.to_string(),
event_type: "orchestrator.run_failed".to_string(),
status: "queued".to_string(),
repo: "acme/platform".to_string(),
workspace_root: "/tmp/acme".to_string(),
title: "Build failure in CI".to_string(),
detail: Some("boom".to_string()),
excerpt: vec!["boom".to_string()],
source: Some("desktop_logs".to_string()),
run_id: None,
session_id: None,
correlation_id: None,
component: Some("orchestrator".to_string()),
level: Some("error".to_string()),
occurrence_count: 1,
created_at_ms: crate::now_ms(),
updated_at_ms: crate::now_ms(),
last_seen_at_ms: Some(crate::now_ms()),
draft_id: Some(draft_id.clone()),
triage_run_id: Some(run_id.clone()),
last_error: None,
duplicate_summary: None,
duplicate_matches: None,
event_payload: None,
})
.await
.expect("seed replay incident");
let replay_req = Request::builder()
.method("POST")
.uri("/bug-monitor/incidents/incident-replay-approved-draft/replay")
.body(Body::empty())
.expect("replay request");
let replay_resp = app
.clone()
.oneshot(replay_req)
.await
.expect("replay response");
assert_eq!(replay_resp.status(), StatusCode::OK);
let replay_payload: Value = serde_json::from_slice(
&to_bytes(replay_resp.into_body(), usize::MAX)
.await
.expect("replay body"),
)
.expect("replay json");
assert_eq!(
replay_payload
.get("run")
.and_then(|row| row.get("run_id"))
.and_then(Value::as_str),
Some(run_id.as_str())
);
assert_eq!(
replay_payload.get("deduped").and_then(Value::as_bool),
Some(true)
);
assert!(replay_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Build failure in CI")));
assert_eq!(
replay_payload
.get("issue_draft_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_issue_draft")
);
assert_eq!(
replay_payload
.get("triage_summary")
.and_then(|row| row.get("suggested_title"))
.and_then(Value::as_str),
Some("Bug Monitor issue")
);
assert_eq!(
replay_payload
.get("triage_summary_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("bug_monitor_triage_summary")
);
assert_eq!(
replay_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
replay_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
}
#[tokio::test]
async fn bug_monitor_triage_run_writes_duplicate_match_artifact() {
let state = test_state().await;
state
.capability_resolver
.refresh_builtin_bindings()
.await
.expect("refresh builtin bindings");
state
.put_bug_monitor_config(crate::BugMonitorConfig {
enabled: true,
repo: Some("acme/platform".to_string()),
workspace_root: Some("/tmp/acme".to_string()),
..Default::default()
})
.await
.expect("config");
let app = app_router(state.clone());
let create_req = Request::builder()
.method("POST")
.uri("/bug-monitor/report")
.header("content-type", "application/json")
.body(Body::from(
json!({
"report": {
"source": "desktop_logs",
"title": "Build failure in CI",
"fingerprint": "manual-artifact-fingerprint-source",
"excerpt": ["Build failure in CI"],
}
})
.to_string(),
))
.expect("create request");
let create_resp = app
.clone()
.oneshot(create_req)
.await
.expect("create response");
assert_eq!(create_resp.status(), StatusCode::OK);
let create_payload: Value = serde_json::from_slice(
&to_bytes(create_resp.into_body(), usize::MAX)
.await
.expect("create body"),
)
.expect("create json");
let draft_fingerprint = create_payload
.get("draft")
.and_then(|row| row.get("fingerprint"))
.and_then(Value::as_str)
.expect("draft fingerprint")
.to_string();
let draft_id = create_payload
.get("draft")
.and_then(|row| row.get("draft_id"))
.and_then(Value::as_str)
.expect("draft id")
.to_string();
let seed_run_req = Request::builder()
.method("POST")
.uri("/coder/runs")
.header("content-type", "application/json")
.body(Body::from(
json!({
"coder_run_id": "coder-run-failure-pattern-artifact",
"workflow_mode": "issue_triage",
"repo_binding": {
"project_id": "proj-engine",
"workspace_id": "ws-tandem",
"workspace_root": "/tmp/tandem-repo",
"repo_slug": "acme/platform"
},
"github_ref": {
"kind": "issue",
"number": 302
}
})
.to_string(),
))
.expect("seed request");
let seed_run_resp = app
.clone()
.oneshot(seed_run_req)
.await
.expect("seed response");
assert_eq!(seed_run_resp.status(), StatusCode::OK);
let candidate_req = Request::builder()
.method("POST")
.uri("/coder/runs/coder-run-failure-pattern-artifact/memory-candidates")
.header("content-type", "application/json")
.body(Body::from(
json!({
"kind": "failure_pattern",
"summary": "Repeated orchestrator failure",
"payload": {
"type": "failure.pattern",
"repo_slug": "acme/platform",
"fingerprint": draft_fingerprint,
"symptoms": ["Build failure in CI"],
"canonical_markers": ["Build failure in CI"],
"linked_issue_numbers": [302],
"linked_pr_numbers": [],
"affected_components": ["ci"],
"artifact_refs": ["artifact://ctx/manual/triage.summary.json"]
}
})
.to_string(),
))
.expect("candidate request");
let candidate_resp = app
.clone()
.oneshot(candidate_req)
.await
.expect("candidate response");
assert_eq!(candidate_resp.status(), StatusCode::OK);
let draft_status = create_payload
.get("draft")
.and_then(|row| row.get("status"))
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
if draft_status.eq_ignore_ascii_case("approval_required") {
let approve_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/approve"))
.header("content-type", "application/json")
.body(Body::from("{}"))
.expect("approve request");
let approve_resp = app
.clone()
.oneshot(approve_req)
.await
.expect("approve response");
assert_eq!(approve_resp.status(), StatusCode::OK);
}
let triage_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/triage-run"))
.body(Body::empty())
.expect("triage request");
let triage_resp = app
.clone()
.oneshot(triage_req)
.await
.expect("triage response");
assert_eq!(triage_resp.status(), StatusCode::OK);
let triage_payload: Value = serde_json::from_slice(
&to_bytes(triage_resp.into_body(), usize::MAX)
.await
.expect("triage body"),
)
.expect("triage json");
let run_id = triage_payload
.get("run")
.and_then(|row| row.get("run_id"))
.and_then(Value::as_str)
.expect("run id");
assert_eq!(
triage_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("failure_duplicate_matches")
);
assert_eq!(
triage_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
triage_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
assert!(triage_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/failure_duplicate_matches.json")));
let issue_draft_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/issue-draft"))
.body(Body::empty())
.expect("issue draft request");
let issue_draft_resp = app
.clone()
.oneshot(issue_draft_req)
.await
.expect("issue draft response");
assert_eq!(issue_draft_resp.status(), StatusCode::OK);
let issue_draft_payload: Value = serde_json::from_slice(
&to_bytes(issue_draft_resp.into_body(), usize::MAX)
.await
.expect("issue draft body"),
)
.expect("issue draft json");
assert_eq!(
issue_draft_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("failure_duplicate_matches")
);
assert_eq!(
issue_draft_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
issue_draft_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
assert!(issue_draft_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/failure_duplicate_matches.json")));
let publish_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/publish"))
.body(Body::empty())
.expect("publish request");
let publish_resp = app
.clone()
.oneshot(publish_req)
.await
.expect("publish response");
assert_eq!(publish_resp.status(), StatusCode::BAD_REQUEST);
let publish_payload: Value = serde_json::from_slice(
&to_bytes(publish_resp.into_body(), usize::MAX)
.await
.expect("publish body"),
)
.expect("publish json");
assert_eq!(
publish_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("failure_duplicate_matches")
);
assert_eq!(
publish_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
publish_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
assert!(publish_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/failure_duplicate_matches.json")));
let recheck_req = Request::builder()
.method("POST")
.uri(format!("/bug-monitor/drafts/{draft_id}/recheck-match"))
.body(Body::empty())
.expect("recheck request");
let recheck_resp = app
.clone()
.oneshot(recheck_req)
.await
.expect("recheck response");
assert_eq!(recheck_resp.status(), StatusCode::BAD_REQUEST);
let recheck_payload: Value = serde_json::from_slice(
&to_bytes(recheck_resp.into_body(), usize::MAX)
.await
.expect("recheck body"),
)
.expect("recheck json");
assert_eq!(
recheck_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("artifact_type"))
.and_then(Value::as_str),
Some("failure_duplicate_matches")
);
assert_eq!(
recheck_payload
.get("duplicate_summary")
.and_then(|row| row.get("match_count"))
.and_then(Value::as_u64),
Some(1)
);
assert_eq!(
recheck_payload
.get("duplicate_matches")
.and_then(Value::as_array)
.map(|rows| rows.len()),
Some(1)
);
assert!(recheck_payload
.get("issue_draft")
.and_then(|row| row.get("rendered_body"))
.and_then(Value::as_str)
.is_some_and(|body| body.contains("Historical duplicate")));
assert!(recheck_payload
.get("duplicate_matches_artifact")
.and_then(|row| row.get("path"))
.and_then(Value::as_str)
.is_some_and(|path| path.ends_with("/artifacts/failure_duplicate_matches.json")));
let get_blackboard_req = Request::builder()
.method("GET")
.uri(format!("/context/runs/{run_id}/blackboard"))
.body(Body::empty())
.expect("get blackboard request");
let get_blackboard_resp = app
.clone()
.oneshot(get_blackboard_req)
.await
.expect("get blackboard response");
assert_eq!(get_blackboard_resp.status(), StatusCode::OK);
let get_blackboard_payload: Value = serde_json::from_slice(
&to_bytes(get_blackboard_resp.into_body(), usize::MAX)
.await
.expect("get blackboard body"),
)
.expect("get blackboard json");
let duplicate_artifact_present = get_blackboard_payload
.get("blackboard")
.and_then(|row| row.get("artifacts"))
.and_then(Value::as_array)
.map(|rows| {
rows.iter().any(|row| {
row.get("artifact_type").and_then(Value::as_str)
== Some("failure_duplicate_matches")
})
})
.unwrap_or(false);
assert!(duplicate_artifact_present);
}