tandem-server 0.5.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use super::*;
use serde_json::{json, Value};
use std::collections::HashSet;
use std::path::Path;

fn artifact_candidate_source_priority(source: &str) -> i64 {
    match source {
        "verified_output" => 3,
        "session_write" => 2,
        "preexisting_output" => 1,
        _ => 0,
    }
}

fn normalized_anchor_variants(value: &str) -> Vec<String> {
    let trimmed = value.trim().to_ascii_lowercase();
    if trimmed.is_empty() {
        return Vec::new();
    }
    let mut variants = HashSet::new();
    variants.insert(trimmed.clone());
    let collapsed = trimmed
        .chars()
        .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { ' ' })
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");
    if !collapsed.is_empty() {
        variants.insert(collapsed);
    }
    let compact = trimmed
        .chars()
        .filter(|ch| ch.is_ascii_alphanumeric())
        .collect::<String>();
    if !compact.is_empty() {
        variants.insert(compact);
    }
    if trimmed.contains('/') {
        if let Some(file_name) = Path::new(&trimmed)
            .file_name()
            .and_then(|value| value.to_str())
        {
            variants.insert(file_name.to_ascii_lowercase());
            variants.extend(
                file_name
                    .chars()
                    .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { ' ' })
                    .collect::<String>()
                    .split_whitespace()
                    .map(str::to_string)
                    .collect::<Vec<_>>(),
            );
        }
        if let Some(stem) = Path::new(&trimmed)
            .file_stem()
            .and_then(|value| value.to_str())
        {
            variants.insert(stem.to_ascii_lowercase());
            variants.extend(
                stem.chars()
                    .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { ' ' })
                    .collect::<String>()
                    .split_whitespace()
                    .map(str::to_string)
                    .collect::<Vec<_>>(),
            );
        }
    }
    variants.into_iter().collect()
}

fn source_anchor_variants(source: &str) -> Vec<String> {
    let trimmed = source.trim();
    if trimmed.is_empty() {
        return Vec::new();
    }
    let mut variants = normalized_anchor_variants(trimmed);
    let without_scheme = trimmed
        .strip_prefix("https://")
        .or_else(|| trimmed.strip_prefix("http://"))
        .unwrap_or(trimmed);
    let host = without_scheme.split('/').next().unwrap_or(without_scheme);
    variants.extend(normalized_anchor_variants(host));
    if let Some(last_segment) = without_scheme.rsplit('/').next() {
        variants.extend(normalized_anchor_variants(last_segment));
    }
    variants.sort();
    variants.dedup();
    variants
}

pub(crate) fn source_evidence_anchor_target(read_paths: &[String], citations: &[String]) -> usize {
    let unique_sources = read_paths
        .iter()
        .chain(citations.iter())
        .map(|value| value.trim().to_ascii_lowercase())
        .filter(|value| !value.is_empty())
        .collect::<HashSet<_>>();
    match unique_sources.len() {
        0 => 0,
        1 => 1,
        _ => 2,
    }
}

pub(crate) fn evidence_anchor_count(
    text: &str,
    read_paths: &[String],
    citations: &[String],
) -> usize {
    let lowered = text.to_ascii_lowercase();
    let mut matched = HashSet::new();
    for source in read_paths.iter().chain(citations.iter()) {
        let source = source.trim();
        if source.is_empty() {
            continue;
        }
        let matched_source = source_anchor_variants(source)
            .into_iter()
            .any(|variant| !variant.is_empty() && lowered.contains(&variant));
        if matched_source {
            matched.insert(source.to_ascii_lowercase());
        }
    }
    matched.len()
}

pub(crate) fn artifact_text_contradicts_successful_web_research(text: &str) -> bool {
    let lowered = text.to_ascii_lowercase();
    [
        "unavailable_in_current_tooling",
        "no websearch/webfetch tools were available",
        "no websearch or webfetch tools were available",
        "no websearch tools were available",
        "no web research tools were available",
        "no external urls could be fetched",
        "external web research could not be performed",
        "web research could not be performed",
        "websearch unavailable",
        "web research unavailable",
        "current external web research could not be performed",
        "no dated market or technical sources were captured",
        "no external sources were captured",
        "no web sources were captured",
        "web research returned no usable output",
    ]
    .iter()
    .any(|needle| lowered.contains(needle))
}

pub(crate) fn assess_artifact_candidate(
    node: &AutomationFlowNode,
    workspace_root: &str,
    source: &str,
    text: &str,
    read_paths: &[String],
    discovered_relevant_paths: &[String],
    upstream_read_paths: &[String],
    upstream_citations: &[String],
) -> ArtifactCandidateAssessment {
    let trimmed = text.trim();
    let length = trimmed.len();
    let placeholder_like = placeholder_like_artifact_text(trimmed);
    let substantive = substantive_artifact_text(trimmed);
    let heading_count = markdown_heading_count(trimmed);
    let list_count = markdown_list_item_count(trimmed);
    let paragraph_count = paragraph_block_count(trimmed);
    let required_section_count = artifact_required_section_count(node, trimmed);
    let reviewed_paths = extract_markdown_section_paths(trimmed, "Files reviewed")
        .into_iter()
        .filter_map(|value| normalize_workspace_display_path(workspace_root, &value))
        .collect::<Vec<_>>();
    let files_not_reviewed = extract_markdown_section_paths(trimmed, "Files not reviewed")
        .into_iter()
        .filter_map(|value| normalize_workspace_display_path(workspace_root, &value))
        .collect::<Vec<_>>();
    let reviewed_paths_backed_by_read = reviewed_paths
        .iter()
        .filter(|path| read_paths.iter().any(|read| read == *path))
        .cloned()
        .collect::<Vec<_>>();
    let files_reviewed_present = files_reviewed_section_lists_paths(trimmed);
    let citation_count = markdown_citation_count(trimmed);
    let web_sources_reviewed_present = web_sources_reviewed_section_lists_sources(trimmed);
    let effective_relevant_paths = if discovered_relevant_paths.is_empty() {
        reviewed_paths.clone()
    } else {
        discovered_relevant_paths.to_vec()
    };
    let evidence_anchor_count =
        evidence_anchor_count(trimmed, upstream_read_paths, upstream_citations);
    let unreviewed_relevant_paths = effective_relevant_paths
        .iter()
        .filter(|path| {
            !read_paths.iter().any(|read| read == *path)
                && !files_not_reviewed.iter().any(|skipped| skipped == *path)
        })
        .cloned()
        .collect::<Vec<_>>();

    let mut score = 0i64;
    score += artifact_candidate_source_priority(source) * 25;
    score += (length.min(12_000) / 24) as i64;
    score += (heading_count as i64) * 60;
    score += (list_count as i64) * 18;
    score += (paragraph_count as i64) * 24;
    score += (required_section_count as i64) * 160;
    score += (evidence_anchor_count.min(5) as i64) * 120;
    if substantive {
        score += 2_000;
    }
    if files_reviewed_present {
        score += 180;
    }
    score += (citation_count.min(8) as i64) * 45;
    if web_sources_reviewed_present {
        score += 140;
    }
    if !reviewed_paths.is_empty() && reviewed_paths.len() == reviewed_paths_backed_by_read.len() {
        score += 260;
    } else if !reviewed_paths_backed_by_read.is_empty() {
        score += 90;
    }
    score -= (unreviewed_relevant_paths.len() as i64) * 220;
    if placeholder_like {
        score -= 450;
    }
    if trimmed.is_empty() {
        score -= 2_000;
    }

    ArtifactCandidateAssessment {
        source: source.to_string(),
        text: text.to_string(),
        length,
        score,
        substantive,
        placeholder_like,
        heading_count,
        list_count,
        paragraph_count,
        required_section_count,
        files_reviewed_present,
        reviewed_paths,
        reviewed_paths_backed_by_read,
        unreviewed_relevant_paths,
        citation_count,
        web_sources_reviewed_present,
        evidence_anchor_count,
    }
}

pub(crate) fn artifact_text_contains_required_tool_mode_failure(text: &str) -> bool {
    let lower = text.to_ascii_lowercase();
    lower.contains("tool_mode_required_not_satisfied")
        || lower.contains("write_required_not_satisfied")
        || lower.contains("tool choice 'required' must be specified with 'tools' parameter")
        || lower.contains("tool choice `required` must be specified with `tools` parameter")
}

fn value_has_nonempty_key(value: &Value, keys: &[&str]) -> bool {
    match value {
        Value::Object(map) => {
            for (key, child) in map {
                if keys
                    .iter()
                    .any(|candidate| key.eq_ignore_ascii_case(candidate))
                {
                    match child {
                        Value::Null => {}
                        Value::Array(items) if items.is_empty() => {}
                        Value::Object(items) if items.is_empty() => {}
                        Value::String(text) if text.trim().is_empty() => {}
                        _ => return true,
                    }
                }
                if value_has_nonempty_key(child, keys) {
                    return true;
                }
            }
            false
        }
        Value::Array(items) => items
            .iter()
            .any(|child| value_has_nonempty_key(child, keys)),
        _ => false,
    }
}

pub(crate) fn artifact_text_has_connector_source_evidence_or_limitation(text: &str) -> bool {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return false;
    }
    if let Ok(value) = serde_json::from_str::<Value>(trimmed) {
        const EVIDENCE_KEYS: &[&str] = &[
            "posts",
            "items",
            "findings",
            "signals",
            "source_url",
            "permalink",
            "selftext",
            "citations",
            "citations_external",
            "sources_reviewed",
            "web_sources_reviewed",
            "tool_evidence",
            "tool_results",
            "search_queries_used",
            "result_excerpt",
        ];
        const LIMITATION_KEYS: &[&str] = &[
            "limitations",
            "source_limitations",
            "connector_limitations",
            "tool_limitations",
        ];
        return value_has_nonempty_key(&value, EVIDENCE_KEYS)
            || value_has_nonempty_key(&value, LIMITATION_KEYS);
    }
    let lower = trimmed.to_ascii_lowercase();
    [
        "https://www.reddit.com/",
        "permalink",
        "source_url",
        "source url",
        "citations",
        "sources reviewed",
        "connector limitation",
        "source limitation",
        "tool limitation",
    ]
    .iter()
    .any(|needle| lower.contains(needle))
}

pub(crate) fn artifact_text_has_receipt_backed_connector_source_evidence(
    text: &str,
    assessment: Option<&ArtifactCandidateAssessment>,
    executed_concrete_mcp_tools: &[String],
    selected_mcp_server_names: &[String],
) -> bool {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return false;
    }
    if artifact_text_has_connector_source_evidence_or_limitation(trimmed) {
        return true;
    }
    let Some(assessment) = assessment else {
        return false;
    };

    let lowered = trimmed.to_ascii_lowercase();
    let has_receipt_anchor = executed_concrete_mcp_tools
        .iter()
        .chain(selected_mcp_server_names.iter())
        .map(|value| value.trim().to_ascii_lowercase())
        .filter(|value| !value.is_empty())
        .any(|value| lowered.contains(&value));
    let has_structural_substance = assessment.substantive
        || assessment.length >= 120
        || assessment.heading_count > 0
        || assessment.list_count >= 2
        || assessment.paragraph_count >= 2;

    if has_receipt_anchor && (has_structural_substance || assessment.length >= 80) {
        return true;
    }
    !assessment.placeholder_like && has_structural_substance
}

pub(crate) fn artifact_text_is_mcp_inventory_only(text: &str) -> bool {
    let trimmed = text.trim();
    let Ok(value) = serde_json::from_str::<Value>(trimmed) else {
        return false;
    };
    let Some(object) = value.as_object() else {
        return false;
    };
    let inventory_keys = [
        "connected_server_names",
        "enabled_server_names",
        "inventory_version",
        "registered_tools",
        "remote_tools",
        "servers",
    ];
    let has_inventory_shape = inventory_keys
        .iter()
        .filter(|key| object.contains_key(**key))
        .count()
        >= 3;
    has_inventory_shape && !artifact_text_has_connector_source_evidence_or_limitation(trimmed)
}

pub(crate) fn best_artifact_candidate(
    candidates: &[ArtifactCandidateAssessment],
) -> Option<ArtifactCandidateAssessment> {
    candidates.iter().cloned().max_by(|left, right| {
        left.score
            .cmp(&right.score)
            .then(left.substantive.cmp(&right.substantive))
            .then(
                left.required_section_count
                    .cmp(&right.required_section_count),
            )
            .then(left.evidence_anchor_count.cmp(&right.evidence_anchor_count))
            .then(left.heading_count.cmp(&right.heading_count))
            .then(left.length.cmp(&right.length))
            .then(
                artifact_candidate_source_priority(&left.source)
                    .cmp(&artifact_candidate_source_priority(&right.source)),
            )
    })
}

pub(crate) fn artifact_candidate_summary(
    candidate: &ArtifactCandidateAssessment,
    accepted: bool,
) -> Value {
    json!({
        "source": candidate.source,
        "length": candidate.length,
        "score": candidate.score,
        "substantive": candidate.substantive,
        "placeholder_like": candidate.placeholder_like,
        "heading_count": candidate.heading_count,
        "list_count": candidate.list_count,
        "paragraph_count": candidate.paragraph_count,
        "required_section_count": candidate.required_section_count,
        "files_reviewed_present": candidate.files_reviewed_present,
        "reviewed_paths_backed_by_read": candidate.reviewed_paths_backed_by_read,
        "unreviewed_relevant_paths": candidate.unreviewed_relevant_paths,
        "citation_count": candidate.citation_count,
        "web_sources_reviewed_present": candidate.web_sources_reviewed_present,
        "evidence_anchor_count": candidate.evidence_anchor_count,
        "accepted": accepted,
    })
}