eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use std::collections::HashSet;

use crate::text::levenshtein_distance;
use serde_json::Value;

use super::all_tools::{all_tools as all_tools_slice, PROFILE_NAMES};
use super::types::{ToolExposure, ToolSpec, ToolStability};

// ---------------------------------------------------------------------------
// Basic helpers
// ---------------------------------------------------------------------------

pub fn all_tools() -> &'static [super::types::ToolSpec] {
    all_tools_slice()
}

pub fn get_tool(name: &str) -> Option<&'static super::types::ToolSpec> {
    all_tools_slice().iter().find(|t| t.name == name)
}

pub fn tool_names() -> Vec<&'static str> {
    all_tools_slice().iter().map(|t| t.name).collect()
}

pub fn tools_for_profile(profile: &str) -> Vec<&'static super::types::ToolSpec> {
    if profile == "full" {
        return all_tools_slice()
            .iter()
            .filter(|t| t.exposure != ToolExposure::Hidden)
            .collect();
    }
    all_tools_slice()
        .iter()
        .filter(|t| t.profiles.contains(&profile))
        .collect()
}

pub fn available_profiles() -> &'static [&'static str] {
    PROFILE_NAMES
}

// ---------------------------------------------------------------------------
// Route-critical tool classification
// ---------------------------------------------------------------------------

/// Tools that participate in routing decisions and must emit structured
/// envelope fields (machine_code, verdict) on every successful response.
///
/// Route-critical tools are preflight/composite tools whose verdict drives
/// downstream action selection. Simple utility tools are NOT in this list
/// and should not be forced into artificial verdicts.
pub const ROUTE_CRITICAL_TOOLS: &[&str] = &[
    "edit_preflight",
    "command_preflight",
    "config_preflight",
    "patch_apply_check",
    "text_security_inspect",
];

/// Returns `true` if `name` is a route-critical tool.
pub fn is_route_critical(name: &str) -> bool {
    ROUTE_CRITICAL_TOOLS.contains(&name)
}

pub fn tool_handler_for(name: &str) -> Option<super::types::ToolHandler> {
    get_tool(name).map(|spec| spec.handler)
}

pub fn tool_count() -> usize {
    all_tools_slice().len()
}

pub fn input_schema_for(name: &str) -> Option<Value> {
    get_tool(name).map(|spec| (spec.input_schema)())
}

pub fn output_schema_for(name: &str) -> Option<Value> {
    get_tool(name).map(|spec| (spec.output_schema)())
}

// ---------------------------------------------------------------------------
// MCP tool definition generation
// ---------------------------------------------------------------------------

pub fn mcp_tool_definitions() -> Vec<super::types::ToolDefinition> {
    all_tools_slice()
        .iter()
        .filter(|t| t.exposure != ToolExposure::Hidden)
        .map(|spec| {
            let deprecated = if spec.stability == ToolStability::Deprecated {
                Some(true)
            } else {
                None
            };
            super::types::ToolDefinition {
                name: spec.name.to_string(),
                description: spec.description.to_string(),
                input_schema: (spec.input_schema)(),
                output_schema: Some((spec.output_schema)()),
                tier: Some(spec.tier),
                tags: Some(spec.tags.iter().map(|s| s.to_string()).collect()),
                deprecated,
                category: Some(spec.category.to_string()),
                llm_exposure: Some(spec.exposure.as_str().to_string()),
                cost: Some(spec.cost.as_str().to_string()),
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tool list filtering (tools/list handler logic)
// ---------------------------------------------------------------------------

/// Options for filtering tool definitions in tools/list.
pub struct ToolListOptions<'a> {
    pub profile: &'a str,
    pub names: Option<&'a [String]>,
    pub tier: Option<u8>,
    pub tags: Option<&'a [String]>,
    pub schema_detail: &'a str,
    pub audience: Option<ToolListAudience>,
}

/// Filter tool definitions by profile, names, tier, tags, and schema detail.
///
/// This is the core listing logic that was previously in server.rs.
/// It returns a `Vec<ToolDefinition>` ready for MCP serialization.
pub fn list_tool_definitions(options: ToolListOptions<'_>) -> Vec<super::types::ToolDefinition> {
    let specs = filtered_specs(&options);
    let mut tools: Vec<super::types::ToolDefinition> = specs
        .into_iter()
        .map(|spec| {
            let deprecated = if spec.stability == ToolStability::Deprecated {
                Some(true)
            } else {
                None
            };
            super::types::ToolDefinition {
                name: spec.name.to_string(),
                description: spec.description.to_string(),
                input_schema: (spec.input_schema)(),
                output_schema: Some((spec.output_schema)()),
                tier: Some(spec.tier),
                tags: Some(spec.tags.iter().map(|s| s.to_string()).collect()),
                deprecated,
                category: Some(spec.category.to_string()),
                llm_exposure: Some(spec.exposure.as_str().to_string()),
                cost: Some(spec.cost.as_str().to_string()),
            }
        })
        .collect();

    // Filter by names
    if let Some(names) = options.names {
        let name_set: HashSet<&str> = names.iter().map(|s| s.as_str()).collect();
        tools.retain(|t| name_set.contains(t.name.as_str()));
    }

    // Filter by tier
    if let Some(tier) = options.tier {
        tools.retain(|t| t.tier == Some(tier));
    }

    // Filter by tags (all specified tags must be present)
    if let Some(tags) = options.tags {
        let tag_set: HashSet<&str> = tags.iter().map(|s| s.as_str()).collect();
        tools.retain(|t| {
            if let Some(ref tool_tags) = t.tags {
                tag_set
                    .iter()
                    .all(|tag| tool_tags.iter().any(|tt| tt.as_str() == *tag))
            } else {
                false
            }
        });
    }

    if options.schema_detail == "compact" {
        for tool in &mut tools {
            // Truncate description to 120 chars
            if tool.description.chars().count() > 120 {
                let truncated: String = tool.description.chars().take(117).collect();
                tool.description = truncated;
                tool.description.push_str("...");
            }
            // Compact input schema: strip defaults, truncate property descriptions
            tool.input_schema = compact_input_schema(&tool.input_schema);
            // Compact output schema: keep top-level keys/types only
            if let Some(output) = tool.output_schema.take() {
                tool.output_schema = Some(compact_output_schema(&output));
            }
            // Python compact mode: drops tier and tags, keeps category/llm_exposure/cost
            tool.tier = None;
            tool.tags = None;
        }
    } else {
        // Non-compact mode: include deprecated field for all tools (Python parity)
        for tool in &mut tools {
            tool.deprecated = Some(tool.deprecated.unwrap_or(false));
        }
    }

    tools
}

/// Filter tools and serialize in the modern (2026-07-28) Tool shape.
///
/// Same profile/audience/names/tier/tags filtering and registry ordering as
/// `list_tool_definitions`, but emits protocol-standard top-level fields plus
/// `annotations` and a namespaced `_meta` object instead of nonstandard
/// top-level registry keys. Legacy serialization is untouched.
pub fn list_modern_tool_values(options: ToolListOptions<'_>) -> Vec<Value> {
    let specs = filtered_specs(&options);
    let compact = options.schema_detail == "compact";

    specs
        .into_iter()
        .map(|spec| {
            let mut description = spec.description.to_string();
            if compact && description.chars().count() > 120 {
                let truncated: String = description.chars().take(117).collect();
                description = truncated;
                description.push_str("...");
            }
            let mut input_schema = (spec.input_schema)();
            if compact {
                input_schema = compact_input_schema(&input_schema);
            }
            let mut output_schema = Some((spec.output_schema)());
            if compact {
                if let Some(output) = output_schema.take() {
                    output_schema = Some(compact_output_schema(&output));
                }
            }
            super::types::modern_tool_value(spec, description, input_schema, output_schema, compact)
        })
        .collect()
}

fn filtered_specs(options: &ToolListOptions<'_>) -> Vec<&'static ToolSpec> {
    let name_set: Option<HashSet<&str>> = options
        .names
        .map(|names| names.iter().map(String::as_str).collect());
    let tag_set: Option<HashSet<&str>> = options
        .tags
        .map(|tags| tags.iter().map(String::as_str).collect());

    all_tools_slice()
        .iter()
        .filter(|spec| {
            let in_profile = if options.profile == "full" {
                spec.exposure != ToolExposure::Hidden
            } else {
                spec.profiles.contains(&options.profile)
            };
            let in_audience = match options.audience {
                None | Some(ToolListAudience::Debug) => true,
                Some(ToolListAudience::Model) => spec.exposure != ToolExposure::HarnessOnly,
                Some(ToolListAudience::Harness) => spec.exposure != ToolExposure::Hidden,
            };
            let name_matches = name_set
                .as_ref()
                .is_none_or(|names| names.contains(spec.name));
            let tier_matches = options.tier.is_none_or(|tier| spec.tier == tier);
            let tags_match = tag_set
                .as_ref()
                .is_none_or(|tags| tags.iter().all(|tag| spec.tags.iter().any(|t| t == tag)));
            in_profile && in_audience && name_matches && tier_matches && tags_match
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Audience-aware listing
// ---------------------------------------------------------------------------

/// Audience for tool listing, controlling which exposure levels are included.
///
/// - `Model`: Excludes `HarnessOnly` and `Hidden`. Safe for ordinary model-visible use.
/// - `Harness`: Includes `HarnessOnly` tools for selected profiles but excludes `Hidden`.
/// - `Debug`: Includes all non-hidden tools, including `ExpertOnly` and `HarnessOnly`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ToolListAudience {
    Model,
    Harness,
    Debug,
}

/// Filter tools by profile and audience.
///
/// For the `full` profile, hidden tools are always excluded.
/// For other profiles, only tools in the profile's `profiles` list are included.
/// The audience then further filters by exposure level.
pub fn tools_for_profile_audience(
    profile: &str,
    audience: ToolListAudience,
) -> Vec<&'static super::types::ToolSpec> {
    let profile_tools = tools_for_profile(profile);
    match audience {
        ToolListAudience::Model => profile_tools
            .into_iter()
            .filter(|t| t.exposure != ToolExposure::HarnessOnly)
            .collect(),
        ToolListAudience::Harness => profile_tools
            .into_iter()
            .filter(|t| t.exposure != ToolExposure::Hidden)
            .collect(),
        ToolListAudience::Debug => profile_tools,
    }
}

/// Get tool names for a profile and audience combination.
pub fn tool_names_for_profile_audience(
    profile: &str,
    audience: ToolListAudience,
) -> Vec<&'static str> {
    tools_for_profile_audience(profile, audience)
        .into_iter()
        .map(|spec| spec.name)
        .collect()
}

// ---------------------------------------------------------------------------
// Schema compaction
// ---------------------------------------------------------------------------

pub fn compact_input_schema(schema: &Value) -> Value {
    let obj = match schema.as_object() {
        Some(o) => o,
        None => return schema.clone(),
    };

    let mut compact = serde_json::Map::new();
    compact.insert(
        "type".to_string(),
        obj.get("type")
            .cloned()
            .unwrap_or_else(|| Value::String("object".to_string())),
    );

    if let Some(props) = obj.get("properties").and_then(|v| v.as_object()) {
        let mut compact_props = serde_json::Map::new();
        for (prop_name, prop_def) in props {
            if let Some(prop_obj) = prop_def.as_object() {
                let mut cp = serde_json::Map::new();
                if let Some(t) = prop_obj.get("type") {
                    cp.insert("type".to_string(), t.clone());
                }
                if let Some(e) = prop_obj.get("enum") {
                    cp.insert("enum".to_string(), e.clone());
                }
                if let Some(r) = prop_obj.get("required") {
                    cp.insert("required".to_string(), r.clone());
                }
                if let Some(items) = prop_obj.get("items") {
                    cp.insert("items".to_string(), items.clone());
                }
                for key in &[
                    "minimum",
                    "maximum",
                    "exclusiveMinimum",
                    "exclusiveMaximum",
                    "minLength",
                    "maxLength",
                    "pattern",
                    "minItems",
                    "maxItems",
                    "multipleOf",
                ] {
                    if let Some(v) = prop_obj.get(*key) {
                        cp.insert(key.to_string(), v.clone());
                    }
                }
                if let Some(desc) = prop_obj.get("description").and_then(|v| v.as_str()) {
                    let truncated = if desc.chars().count() > 80 {
                        format!("{}...", desc.chars().take(77).collect::<String>())
                    } else {
                        desc.to_string()
                    };
                    cp.insert("description".to_string(), Value::String(truncated));
                }
                compact_props.insert(prop_name.clone(), Value::Object(cp));
            } else {
                compact_props.insert(prop_name.clone(), prop_def.clone());
            }
        }
        compact.insert("properties".to_string(), Value::Object(compact_props));
    }

    if let Some(req) = obj.get("required") {
        compact.insert("required".to_string(), req.clone());
    }

    Value::Object(compact)
}

pub fn compact_output_schema(schema: &Value) -> Value {
    let obj = match schema.as_object() {
        Some(o) => o,
        None => return serde_json::json!({"type": "object"}),
    };

    let mut compact_output = serde_json::json!({"type": obj.get("type").unwrap_or(&Value::String("object".to_string()))});
    if let Some(props) = obj.get("properties").and_then(|v| v.as_object()) {
        let mut compact_props = serde_json::Map::new();
        for (key, prop) in props {
            let mut compact_prop = serde_json::json!({});
            if let Some(t) = prop.get("type") {
                compact_prop["type"] = t.clone();
            }
            if let Some(e) = prop.get("enum") {
                compact_prop["enum"] = e.clone();
            }
            compact_props.insert(key.clone(), compact_prop);
        }
        compact_output["properties"] = Value::Object(compact_props);
    }

    compact_output
}

// ---------------------------------------------------------------------------
// Suggestions
// ---------------------------------------------------------------------------

pub fn find_close_match<'a>(input: &str, tool_names: &[&'a str]) -> Option<&'a str> {
    if input.len() > 200 {
        return None;
    }
    let lower_input = input.to_lowercase();

    for &name in tool_names {
        if name.to_lowercase() == lower_input {
            return Some(name);
        }
    }

    fn at_word_boundary(sub: &str, s: &str) -> bool {
        if let Some(idx) = s.find(sub) {
            if idx == 0 {
                return true;
            }
            s.as_bytes().get(idx - 1) == Some(&b'_') || s.as_bytes().get(idx - 1) == Some(&b'-')
        } else {
            false
        }
    }

    let mut best_boundary: Option<(&str, usize)> = None;
    for &name in tool_names {
        let lower_name = name.to_lowercase();
        if at_word_boundary(&lower_input, &lower_name)
            || at_word_boundary(&lower_name, &lower_input)
        {
            let is_shorter = match best_boundary {
                Some((best_name, _)) => name.len() < best_name.len(),
                None => true,
            };
            if is_shorter {
                best_boundary = Some((name, 0));
            }
        }
    }
    if let Some((name, _)) = best_boundary {
        return Some(name);
    }

    let mut best: Option<(&str, usize)> = None;
    for &name in tool_names {
        let dist = levenshtein_distance(input, name);
        let threshold = input.chars().count().min(name.chars().count()) / 2;
        if dist <= threshold && best.is_none_or(|(_, best_dist)| dist < best_dist) {
            best = Some((name, dist));
        }
    }

    best.map(|(name, _)| name)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn list_all_tools_for_full_profile() {
        let options = ToolListOptions {
            profile: "full",
            names: None,
            tier: None,
            tags: None,
            schema_detail: "normal",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        assert!(!tools.is_empty());
        // full profile should exclude hidden tools
        for tool in &tools {
            assert_ne!(tool.llm_exposure.as_deref(), Some("hidden"));
        }
    }

    #[test]
    fn list_tool_definitions_names_filter() {
        let options = ToolListOptions {
            profile: "full",
            names: Some(&[String::from("math_eval"), String::from("text_equal")]),
            tier: None,
            tags: None,
            schema_detail: "normal",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        assert_eq!(tools.len(), 2);
        assert!(tools.iter().any(|t| t.name == "math_eval"));
        assert!(tools.iter().any(|t| t.name == "text_equal"));
    }

    #[test]
    fn list_tool_definitions_tier_filter() {
        let options = ToolListOptions {
            profile: "full",
            names: None,
            tier: Some(0),
            tags: None,
            schema_detail: "normal",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        for tool in &tools {
            assert_eq!(tool.tier, Some(0));
        }
    }

    #[test]
    fn list_tool_definitions_compact_schema() {
        let options = ToolListOptions {
            profile: "full",
            names: Some(&[String::from("math_eval")]),
            tier: None,
            tags: None,
            schema_detail: "compact",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        assert_eq!(tools.len(), 1);
        let tool = &tools[0];
        // Compact mode strips tier and tags
        assert_eq!(tool.tier, None);
        assert_eq!(tool.tags, None);
        // Description should be truncated if > 120 chars
        if tool.description.chars().count() > 120 {
            assert!(tool.description.ends_with("..."));
        }
    }

    #[test]
    fn list_tool_definitions_tags_filter() {
        let options = ToolListOptions {
            profile: "full",
            names: None,
            tier: None,
            tags: Some(&[String::from("math")]),
            schema_detail: "normal",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        for tool in &tools {
            let tags = tool.tags.as_ref().unwrap();
            assert!(
                tags.iter().any(|t| t == "math"),
                "tool {} missing 'math' tag",
                tool.name
            );
        }
    }

    #[test]
    fn list_tool_definitions_profile_filter() {
        let options = ToolListOptions {
            profile: "human_math",
            names: None,
            tier: None,
            tags: None,
            schema_detail: "normal",
            audience: None,
        };
        let tools = list_tool_definitions(options);
        // human_math should have math tools
        assert!(tools.iter().any(|t| t.name == "math_eval"));
        // Should NOT have text tools
        assert!(!tools.iter().any(|t| t.name == "text_equal"));
    }
}