tandem-server 0.6.5

HTTP server for Tandem engine APIs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use serde_json::{json, Value};
use std::path::PathBuf;

use tandem_incident_monitor::github::{GithubToolSet, IncidentMonitorGithubHost};
use tandem_runtime::mcp_ready::{EnsureReadyPolicy, McpReadyError};
use tandem_runtime::McpRemoteTool;
use tandem_types::{EngineEvent, ToolResult};

use crate::{
    AppState, ExternalActionRecord, IncidentMonitorConfig, IncidentMonitorDraftRecord,
    IncidentMonitorIncidentRecord, IncidentMonitorPostRecord, IncidentMonitorStatus,
};

pub use tandem_incident_monitor::github::{
    publish_draft, record_post_failure, GithubDestinationContext, PublishMode, PublishOutcome,
};

const INCIDENT_MONITOR_LABEL: &str = "incident-monitor";

#[async_trait::async_trait]
impl IncidentMonitorGithubHost for AppState {
    async fn incident_monitor_status_snapshot(&self) -> IncidentMonitorStatus {
        AppState::incident_monitor_status_snapshot(self).await
    }

    async fn get_incident_monitor_draft(
        &self,
        draft_id: &str,
    ) -> Option<IncidentMonitorDraftRecord> {
        AppState::get_incident_monitor_draft(self, draft_id).await
    }

    async fn put_incident_monitor_draft(
        &self,
        draft: IncidentMonitorDraftRecord,
    ) -> anyhow::Result<IncidentMonitorDraftRecord> {
        AppState::put_incident_monitor_draft(self, draft).await
    }

    async fn get_incident_monitor_incident(
        &self,
        incident_id: &str,
    ) -> Option<IncidentMonitorIncidentRecord> {
        AppState::get_incident_monitor_incident(self, incident_id).await
    }

    async fn put_incident_monitor_post(
        &self,
        post: IncidentMonitorPostRecord,
    ) -> anyhow::Result<IncidentMonitorPostRecord> {
        AppState::put_incident_monitor_post(self, post).await
    }

    async fn list_incident_monitor_posts(&self, limit: usize) -> Vec<IncidentMonitorPostRecord> {
        AppState::list_incident_monitor_posts(self, limit).await
    }

    async fn list_incident_monitor_posts_by_draft(
        &self,
        draft_id: &str,
    ) -> Vec<IncidentMonitorPostRecord> {
        let mut rows = self
            .incident_monitor_posts
            .read()
            .await
            .values()
            .filter(|post| post.draft_id == draft_id)
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by_key(|post| std::cmp::Reverse(post.updated_at_ms));
        rows
    }

    async fn list_incident_monitor_posts_by_fingerprint(
        &self,
        repo: &str,
        fingerprint: &str,
    ) -> Vec<IncidentMonitorPostRecord> {
        let mut rows = self
            .incident_monitor_posts
            .read()
            .await
            .values()
            .filter(|post| post.repo == repo && post.fingerprint == fingerprint)
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by_key(|post| std::cmp::Reverse(post.updated_at_ms));
        rows
    }

    async fn list_incident_monitor_posts_by_idempotency_key(
        &self,
        idempotency_key: &str,
    ) -> Vec<IncidentMonitorPostRecord> {
        let mut rows = self
            .incident_monitor_posts
            .read()
            .await
            .values()
            .filter(|post| post.idempotency_key == idempotency_key)
            .cloned()
            .collect::<Vec<_>>();
        rows.sort_by_key(|post| std::cmp::Reverse(post.updated_at_ms));
        rows
    }

    async fn try_claim_incident_monitor_post_idempotency(
        &self,
        post: IncidentMonitorPostRecord,
    ) -> anyhow::Result<(bool, IncidentMonitorPostRecord)> {
        AppState::try_claim_incident_monitor_post_idempotency(self, post).await
    }

    async fn mirror_incident_monitor_post_as_external_action(
        &self,
        draft: &IncidentMonitorDraftRecord,
        post: &IncidentMonitorPostRecord,
    ) {
        let capability_id = match post.operation.as_str() {
            "comment_issue" => Some("github.comment_on_issue".to_string()),
            "create_issue" => Some("github.create_issue".to_string()),
            _ => None,
        };
        let action = ExternalActionRecord {
            action_id: post.post_id.clone(),
            operation: post.operation.clone(),
            status: post.status.clone(),
            source_kind: Some("incident_monitor".to_string()),
            source_id: Some(draft.draft_id.clone()),
            routine_run_id: None,
            context_run_id: draft.triage_run_id.clone(),
            capability_id,
            provider: Some(INCIDENT_MONITOR_LABEL.to_string()),
            target: Some(
                post.target_ref
                    .clone()
                    .unwrap_or_else(|| draft.repo.clone()),
            ),
            approval_state: Some(if draft.status.eq_ignore_ascii_case("approval_required") {
                "approval_required".to_string()
            } else {
                "executed".to_string()
            }),
            idempotency_key: Some(post.idempotency_key.clone()),
            receipt: Some(json!({
                "post_id": post.post_id,
                "draft_id": post.draft_id,
                "incident_id": post.incident_id,
                "destination_id": post.destination_id,
                "destination_kind": post.destination_kind,
                "route_id": post.route_id,
                "route_match_reason": post.route_match_reason,
                "issue_number": post.issue_number,
                "issue_url": post.issue_url,
                "comment_id": post.comment_id,
                "comment_url": post.comment_url,
                "external_id": post.external_id,
                "external_url": post.external_url,
                "external_title": post.external_title,
                "target_ref": post.target_ref,
                "response_excerpt": post.response_excerpt,
            })),
            error: post.error.clone(),
            metadata: Some(json!({
                "repo": post.repo,
                "destination_id": post.destination_id,
                "destination_kind": post.destination_kind,
                "route_id": post.route_id,
                "route_match_reason": post.route_match_reason,
                "target_ref": post.target_ref,
                "fingerprint": post.fingerprint,
                "evidence_digest": post.evidence_digest,
                "confidence": post.confidence,
                "risk_level": post.risk_level,
                "risk_category": draft.risk_category,
                "actor": draft.actor,
                "model": draft.model,
                "tool_name": draft.tool_name,
                "action": draft.action,
                "policy": draft.policy,
                "approval_state": draft.approval_state,
                "blast_radius": draft.blast_radius,
                "external_correlation_ids": draft.external_correlation_ids,
                "expected_destination": post.expected_destination,
                "evidence_refs": post.evidence_refs,
                "quality_gate": post.quality_gate,
                "incident_monitor_operation": post.operation,
            })),
            created_at_ms: post.created_at_ms,
            updated_at_ms: post.updated_at_ms,
        };
        if let Err(error) = AppState::record_external_action(self, action).await {
            tracing::warn!(
                "failed to persist external action mirror for incident monitor post {}: {}",
                post.post_id,
                error
            );
        }
    }

    async fn update_last_post_result(&self, result: String) {
        self.update_incident_monitor_runtime_status(|runtime| {
            runtime.last_post_result = Some(result);
        })
        .await;
    }

    fn publish_event(&self, event: EngineEvent) {
        self.event_bus.publish(event);
    }

    async fn ensure_incident_monitor_issue_draft(
        &self,
        draft_id: &str,
        force: bool,
    ) -> anyhow::Result<Value> {
        crate::http::incident_monitor::ensure_incident_monitor_issue_draft(
            self.clone(),
            draft_id,
            force,
        )
        .await
    }

    async fn load_incident_monitor_issue_draft_artifact(
        &self,
        triage_run_id: &str,
    ) -> Option<Value> {
        crate::http::incident_monitor::load_incident_monitor_issue_draft_artifact(
            self,
            triage_run_id,
        )
        .await
    }

    async fn resolve_github_tool_set(
        &self,
        config: &IncidentMonitorConfig,
    ) -> anyhow::Result<GithubToolSet> {
        resolve_github_tool_set_for_state(self, config).await
    }

    async fn call_mcp_tool(
        &self,
        server_name: &str,
        tool_name: &str,
        payload: Value,
    ) -> anyhow::Result<ToolResult> {
        self.mcp
            .call_tool(server_name, tool_name, payload)
            .await
            .map_err(anyhow::Error::msg)
    }

    fn context_run_events_path(&self, run_id: &str) -> PathBuf {
        crate::http::context_runs::context_run_events_path(self, run_id)
    }
}

async fn resolve_github_tool_set_for_state(
    state: &AppState,
    config: &IncidentMonitorConfig,
) -> anyhow::Result<GithubToolSet> {
    let server_name = config
        .mcp_server
        .as_ref()
        .filter(|value| !value.trim().is_empty())
        .ok_or_else(|| anyhow::anyhow!("Incident Monitor MCP server is not configured"))?
        .to_string();
    state
        .mcp
        .ensure_ready(&server_name, EnsureReadyPolicy::with_retries(3, 750))
        .await
        .map_err(|error| match error {
            McpReadyError::NotFound => {
                anyhow::anyhow!("Incident Monitor MCP server `{server_name}` was not found")
            }
            McpReadyError::Disabled => {
                anyhow::anyhow!("Incident Monitor MCP server `{server_name}` is disabled")
            }
            McpReadyError::PermanentlyFailed { last_error } => {
                let detail = last_error.unwrap_or_else(|| "connect failed".to_string());
                anyhow::anyhow!(
                    "Incident Monitor MCP server `{server_name}` was not ready for GitHub publish: {detail}"
                )
            }
        })?;
    let server_tools = state.mcp.server_tools(&server_name).await;
    if server_tools.is_empty() {
        anyhow::bail!("no MCP tools were discovered for selected Incident Monitor server");
    }
    let discovered = state
        .capability_resolver
        .discover_from_runtime(server_tools.clone(), Vec::new())
        .await;
    let mut resolved = state
        .capability_resolver
        .resolve(
            crate::capability_resolver::CapabilityResolveInput {
                workflow_id: Some("incident-monitor-github".to_string()),
                required_capabilities: vec![
                    "github.list_issues".to_string(),
                    "github.get_issue".to_string(),
                    "github.create_issue".to_string(),
                    "github.comment_on_issue".to_string(),
                ],
                optional_capabilities: Vec::new(),
                provider_preference: vec!["mcp".to_string()],
                available_tools: discovered,
            },
            Vec::new(),
        )
        .await?;
    if !resolved.missing_required.is_empty() {
        let _ = state.capability_resolver.refresh_builtin_bindings().await;
        let discovered = state
            .capability_resolver
            .discover_from_runtime(server_tools.clone(), Vec::new())
            .await;
        resolved = state
            .capability_resolver
            .resolve(
                crate::capability_resolver::CapabilityResolveInput {
                    workflow_id: Some("incident-monitor-github".to_string()),
                    required_capabilities: vec![
                        "github.list_issues".to_string(),
                        "github.get_issue".to_string(),
                        "github.create_issue".to_string(),
                        "github.comment_on_issue".to_string(),
                    ],
                    optional_capabilities: Vec::new(),
                    provider_preference: vec!["mcp".to_string()],
                    available_tools: discovered,
                },
                Vec::new(),
            )
            .await?;
    }
    let tool_name = |capability_id: &str| -> anyhow::Result<String> {
        let namespaced = resolved
            .resolved
            .iter()
            .find(|row| row.capability_id == capability_id)
            .map(|row| row.tool_name.clone())
            .ok_or_else(|| anyhow::anyhow!("missing resolved tool for {capability_id}"))?;
        map_namespaced_to_raw_tool(&server_tools, &namespaced)
    };
    let direct_tool_name_fallback = |candidates: &[&str]| -> Option<String> {
        server_tools
            .iter()
            .find(|row| {
                candidates.iter().any(|candidate| {
                    row.tool_name.eq_ignore_ascii_case(candidate)
                        || row.namespaced_name.eq_ignore_ascii_case(candidate)
                })
            })
            .map(|row| row.tool_name.clone())
    };
    let list_issues = tool_name("github.list_issues").or_else(|_| {
        direct_tool_name_fallback(&[
            "list_issues",
            "list_repository_issues",
            "mcp.github.list_issues",
            "mcp.githubcopilot.list_issues",
        ])
        .ok_or_else(|| anyhow::anyhow!("missing resolved tool for github.list_issues"))
    })?;
    let get_issue = tool_name("github.get_issue").or_else(|_| {
        direct_tool_name_fallback(&[
            "get_issue",
            "issue_read",
            "mcp.github.get_issue",
            "mcp.github.issue_read",
            "mcp.githubcopilot.issue_read",
        ])
        .ok_or_else(|| anyhow::anyhow!("missing resolved tool for github.get_issue"))
    })?;
    let create_issue = tool_name("github.create_issue").or_else(|_| {
        direct_tool_name_fallback(&[
            "create_issue",
            "issue_write",
            "mcp.github.create_issue",
            "mcp.github.issue_write",
            "mcp.githubcopilot.issue_write",
        ])
        .ok_or_else(|| anyhow::anyhow!("missing resolved tool for github.create_issue"))
    })?;
    let comment_on_issue = tool_name("github.comment_on_issue").or_else(|_| {
        direct_tool_name_fallback(&[
            "add_issue_comment",
            "create_issue_comment",
            "mcp.github.add_issue_comment",
            "mcp.github.create_issue_comment",
            "mcp.githubcopilot.add_issue_comment",
            "github.comment_on_issue",
        ])
        .ok_or_else(|| anyhow::anyhow!("missing resolved tool for github.comment_on_issue"))
    })?;
    Ok(GithubToolSet {
        server_name,
        list_issues,
        get_issue,
        create_issue,
        comment_on_issue,
    })
}

fn map_namespaced_to_raw_tool(
    tools: &[McpRemoteTool],
    namespaced_name: &str,
) -> anyhow::Result<String> {
    tools
        .iter()
        .find(|row| row.namespaced_name == namespaced_name)
        .map(|row| row.tool_name.clone())
        .ok_or_else(|| anyhow::anyhow!("failed to map MCP tool `{namespaced_name}` to raw tool"))
}