Skip to main content

hematite/agent/
routing.rs

1use super::conversation::WorkflowMode;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4pub(crate) enum QueryIntentClass {
5    ProductTruth,
6    RuntimeDiagnosis,
7    RepoArchitecture,
8    Toolchain,
9    Capability,
10    Implementation,
11    Unknown,
12}
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub(crate) enum DirectAnswerKind {
16    LanguageCapability,
17    UnsafeWorkflowPressure,
18    SessionMemory,
19    RecoveryRecipes,
20    McpLifecycle,
21    AuthorizationPolicy,
22    ToolClasses,
23    ToolRegistryOwnership,
24    SessionResetSemantics,
25    ProductSurface,
26    ReasoningSplit,
27    Identity,
28    WorkflowModes,
29    GemmaNative,
30    GemmaNativeSettings,
31    VerifyProfiles,
32    Toolchain,
33    ArchitectSessionResetPlan,
34}
35
36#[derive(Clone, Copy, Debug)]
37pub(crate) struct QueryIntent {
38    pub(crate) primary_class: QueryIntentClass,
39    pub(crate) direct_answer: Option<DirectAnswerKind>,
40    pub(crate) grounded_trace_mode: bool,
41    pub(crate) capability_mode: bool,
42    pub(crate) capability_needs_repo: bool,
43    pub(crate) toolchain_mode: bool,
44    pub(crate) host_inspection_mode: bool,
45    pub(crate) preserve_project_map_output: bool,
46    pub(crate) architecture_overview_mode: bool,
47}
48
49fn contains_any(haystack: &str, needles: &[&str]) -> bool {
50    needles.iter().any(|needle| haystack.contains(needle))
51}
52
53fn contains_all(haystack: &str, needles: &[&str]) -> bool {
54    needles.iter().all(|needle| haystack.contains(needle))
55}
56
57fn mentions_reset_commands(lower: &str) -> bool {
58    contains_all(lower, &["/clear", "/new", "/forget"])
59}
60
61fn mentions_stable_product_surface(lower: &str) -> bool {
62    contains_any(
63        lower,
64        &[
65            "stable product-surface question",
66            "stable product surface question",
67            "stable product-surface questions",
68            "stable product surface questions",
69        ],
70    )
71}
72
73fn mentions_product_truth_routing(lower: &str) -> bool {
74    let asks_decision_policy = contains_any(
75        lower,
76        &[
77            "how hematite decides",
78            "how does hematite decide",
79            "decides whether",
80            "decide whether",
81        ],
82    );
83    let asks_direct_vs_inspect_split = contains_any(
84        lower,
85        &[
86            "answered as stable product truth",
87            "stable product truth",
88            "stable product behavior",
89            "answer directly",
90            "direct answer",
91            "inspect the repository",
92            "inspect repository",
93            "repository implementation",
94            "repo implementation",
95        ],
96    );
97    asks_decision_policy && asks_direct_vs_inspect_split
98}
99
100fn mentions_broad_system_walkthrough(lower: &str) -> bool {
101    let asks_walkthrough = contains_any(
102        lower,
103        &[
104            "walk me through",
105            "walk through",
106            "how hematite is wired",
107            "understand how hematite is wired",
108            "major runtime pieces",
109            "normal message moves",
110            "moves from the tui to the model and back",
111        ],
112    );
113    let asks_multiple_runtime_areas = contains_any(
114        lower,
115        &[
116            "session recovery",
117            "tool policy",
118            "mcp state",
119            "mcp policy",
120            "files own the major runtime pieces",
121            "which files own",
122            "where session recovery",
123            "where tool policy",
124            "where mcp state",
125        ],
126    );
127    asks_walkthrough && asks_multiple_runtime_areas
128}
129
130fn mentions_capability_question(lower: &str) -> bool {
131    contains_any(
132        lower,
133        &[
134            "what can you do",
135            "what are you capable",
136            "can you make projects",
137            "can you build projects",
138            "do you know other coding languages",
139            "other coding languages",
140            "what languages",
141            "can you use the internet",
142            "internet research capabilities",
143            "what tools do you have",
144        ],
145    )
146}
147
148fn capability_question_requires_repo_inspection(lower: &str) -> bool {
149    contains_any(
150        lower,
151        &[
152            "this repo",
153            "this repository",
154            "codebase",
155            "which files",
156            "implementation",
157            "in this project",
158        ],
159    )
160}
161
162fn mentions_host_inspection_question(lower: &str) -> bool {
163    let host_scope = contains_any(
164        lower,
165        &[
166            "path",
167            "developer tools",
168            "toolchains",
169            "installed",
170            "desktop",
171            "downloads",
172            "folder",
173            "directory",
174            "local development",
175            "machine",
176            "computer",
177        ],
178    );
179    let host_action = contains_any(
180        lower,
181        &[
182            "inspect",
183            "count",
184            "tell me",
185            "summarize",
186            "how big",
187            "biggest",
188            "versions",
189            "duplicate",
190            "missing",
191            "ready",
192        ],
193    );
194
195    host_scope && host_action
196}
197
198pub(crate) fn preferred_host_inspection_topic(user_input: &str) -> Option<&'static str> {
199    let lower = user_input.to_lowercase();
200    let asks_path = lower.contains("path");
201    let asks_toolchains = lower.contains("developer tools")
202        || lower.contains("toolchains")
203        || (lower.contains("installed") && lower.contains("version"))
204        || (lower.contains("detect") && lower.contains("version"));
205    let asks_ports = lower.contains("listening on port")
206        || lower.contains("listening port")
207        || lower.contains("open port")
208        || lower.contains("port 3000")
209        || lower.contains("port ")
210        || lower.contains("listening on ")
211        || lower.contains("exposed")
212        || lower.contains("what is listening");
213    let asks_repo_doctor = lower.contains("repo doctor")
214        || lower.contains("repository doctor")
215        || lower.contains("workspace health")
216        || lower.contains("repo health")
217        || lower.contains("workspace sanity")
218        || (lower.contains("git state")
219            && (lower.contains("release artifacts")
220                || lower.contains("build markers")
221                || lower.contains("hematite memory")));
222    let asks_directory = lower.contains("directory")
223        || lower.contains("folder")
224        || lower.contains("how big")
225        || lower.contains("biggest");
226    let asks_broad_readiness = lower.contains("local development")
227        || lower.contains("ready for local development")
228        || (lower.contains("machine") && lower.contains("ready"))
229        || (lower.contains("computer") && lower.contains("ready"));
230
231    if (asks_path && asks_toolchains)
232        || (mentions_host_inspection_question(&lower) && asks_broad_readiness)
233    {
234        Some("summary")
235    } else if asks_ports {
236        Some("ports")
237    } else if asks_repo_doctor {
238        Some("repo_doctor")
239    } else if lower.contains("desktop") {
240        Some("desktop")
241    } else if lower.contains("downloads") {
242        Some("downloads")
243    } else if asks_path {
244        Some("path")
245    } else if asks_toolchains {
246        Some("toolchains")
247    } else if asks_directory {
248        Some("directory")
249    } else if mentions_host_inspection_question(&lower) {
250        Some("summary")
251    } else {
252        None
253    }
254}
255
256pub(crate) fn looks_like_mutation_request(user_input: &str) -> bool {
257    let lower = user_input.to_lowercase();
258    [
259        "fix ",
260        "change ",
261        "edit ",
262        "modify ",
263        "update ",
264        "rename ",
265        "refactor ",
266        "patch ",
267        "rewrite ",
268        "implement ",
269        "create a file",
270        "create file",
271        "add a file",
272        "delete ",
273        "remove ",
274        "make the change",
275    ]
276    .iter()
277    .any(|needle| lower.contains(needle))
278}
279
280pub(crate) fn classify_query_intent(workflow_mode: WorkflowMode, user_input: &str) -> QueryIntent {
281    let lower = user_input.to_lowercase();
282    let trimmed = user_input.trim().to_ascii_lowercase();
283
284    let mentions_runtime_trace = contains_any(
285        &lower,
286        &[
287            "trace",
288            "how does",
289            "what are the main runtime subsystems",
290            "how does a user message move",
291            "separate normal assistant output",
292            "session reset behavior",
293            "file references",
294            "event types",
295            "channels",
296        ],
297    );
298    let anti_guess = contains_any(&lower, &["do not guess", "if you are unsure"]);
299    let capability_mode = mentions_capability_question(&lower);
300    let capability_needs_repo =
301        capability_mode && capability_question_requires_repo_inspection(&lower);
302    let host_inspection_mode = preferred_host_inspection_topic(&lower).is_some();
303    let toolchain_mode = contains_any(
304        &lower,
305        &[
306            "tooling discipline",
307            "best read-only toolchain",
308            "identify the best tools you actually have",
309            "concrete read-only investigation plan",
310            "do not execute the plan",
311            "available repo-inspection tools",
312            "tool choice discipline",
313            "what tools would you choose first",
314        ],
315    ) || (lower.contains("which tools") && lower.contains("why"))
316        || (lower.contains("when would you choose") && lower.contains("tool"));
317    let preserve_project_map_output = lower.contains("map_project")
318        || lower.contains("entrypoint")
319        || lower.contains("owner file")
320        || lower.contains("owner files")
321        || lower.contains("project structure")
322        || lower.contains("repository structure")
323        || (lower.contains("architecture")
324            && (lower.contains("repo") || lower.contains("repository")));
325    let architecture_overview_mode = {
326        let architecture_signals = contains_any(
327            &lower,
328            &[
329                "architecture walkthrough",
330                "full architecture",
331                "runtime walkthrough",
332                "control flow",
333                "tool routing",
334                "workflow modes",
335                "repo map behavior",
336                "mcp policy",
337                "prompt budgeting",
338                "compaction",
339                "file ownership",
340                "owner files",
341            ],
342        );
343        let broad = contains_any(
344            &lower,
345            &[
346                "full detailed",
347                "all in one answer",
348                "concrete file ownership",
349                "walk me through",
350                "major runtime pieces",
351                "which files own",
352            ],
353        );
354        (architecture_signals && broad)
355            || (lower.contains("runtime")
356                && lower.contains("workflow")
357                && (lower.contains("architecture") || lower.contains("tool routing")))
358            || mentions_broad_system_walkthrough(&lower)
359    };
360
361    let direct_answer = if matches!(
362        trimmed.as_str(),
363        "who are you" | "who are you?" | "what are you" | "what are you?"
364    ) || (lower.contains("what is hematite") && !lower.contains("lm studio"))
365    {
366        Some(DirectAnswerKind::Identity)
367    } else if (mentions_stable_product_surface(&lower) || mentions_product_truth_routing(&lower))
368        && contains_any(
369            &lower,
370            &[
371                "how hematite answers",
372                "how does hematite answer",
373                "how hematite handles",
374                "how does hematite handle",
375                "how hematite decides",
376                "how does hematite decide",
377                "decides whether",
378                "decide whether",
379            ],
380        )
381    {
382        Some(DirectAnswerKind::ProductSurface)
383    } else if mentions_reset_commands(&lower)
384        && contains_any(
385            &lower,
386            &[
387                "exact difference",
388                "difference between",
389                "explain the exact difference",
390                "what is the difference",
391            ],
392        )
393    {
394        Some(DirectAnswerKind::SessionResetSemantics)
395    } else if (lower.contains("reasoning output") || lower.contains("reasoning"))
396        && contains_any(
397            &lower,
398            &["visible chat output", "visible chat", "chat output"],
399        )
400    {
401        Some(DirectAnswerKind::ReasoningSplit)
402    } else if lower.contains("/ask")
403        && lower.contains("/code")
404        && lower.contains("/architect")
405        && lower.contains("/read-only")
406        && lower.contains("/auto")
407        && contains_any(&lower, &["difference", "differences", "what are"])
408    {
409        Some(DirectAnswerKind::WorkflowModes)
410    } else if lower.contains(".hematite/settings.json")
411        && lower.contains("gemma_native_auto")
412        && lower.contains("gemma_native_formatting")
413    {
414        Some(DirectAnswerKind::GemmaNativeSettings)
415    } else if contains_any(
416        &lower,
417        &[
418            "skip verification",
419            "skip build verification",
420            "commit it immediately",
421            "commit immediately",
422        ],
423    ) && contains_any(
424        &lower,
425        &[
426            "make a code change",
427            "make the change",
428            "change the code",
429            "edit the code",
430            "edit a file",
431            "implement",
432        ],
433    ) {
434        Some(DirectAnswerKind::UnsafeWorkflowPressure)
435    } else if contains_any(&lower, &["/gemma-native", "gemma native"])
436        && contains_any(&lower, &["what does", "what is", "how does", "what do"])
437    {
438        Some(DirectAnswerKind::GemmaNative)
439    } else if lower.contains("verify_build")
440        && lower.contains(".hematite/settings.json")
441        && contains_any(
442            &lower,
443            &["build", "test", "lint", "fix", "verification commands"],
444        )
445    {
446        Some(DirectAnswerKind::VerifyProfiles)
447    } else if (lower.contains("carry forward by default")
448        || lower.contains("session memory should you carry forward")
449        || (lower.contains("carry forward")
450            && contains_any(
451                &lower,
452                &[
453                    "besides the active task",
454                    "blocker",
455                    "compacts",
456                    "recovers from a blocker",
457                    "session state",
458                ],
459            )))
460        && contains_any(
461            &lower,
462            &[
463                "restarted hematite",
464                "restarted",
465                "avoid carrying forward",
466                "session state",
467                "active task",
468                "blocker",
469                "compacts",
470                "recovers from a blocker",
471            ],
472        )
473    {
474        Some(DirectAnswerKind::SessionMemory)
475    } else if contains_any(
476        &lower,
477        &[
478            "recovery recipe",
479            "recovery recipes",
480            "recovery step",
481            "recovery steps",
482        ],
483    ) && contains_any(
484        &lower,
485        &[
486            "blocker",
487            "runtime failure",
488            "degrades",
489            "context window",
490            "context-window",
491            "operator",
492        ],
493    ) {
494        Some(DirectAnswerKind::RecoveryRecipes)
495    } else if !architecture_overview_mode
496        && contains_any(
497            &lower,
498            &[
499                "mcp server health",
500                "mcp runtime state",
501                "mcp lifecycle",
502                "mcp state",
503                "mcp healthy",
504                "mcp degraded",
505                "mcp failed",
506            ],
507        )
508    {
509        Some(DirectAnswerKind::McpLifecycle)
510    } else if contains_any(
511        &lower,
512        &[
513            "allowed, denied, or require approval",
514            "allowed denied or require approval",
515            "allow, ask, or deny",
516            "tool call should be allowed",
517            "authorization logic",
518            "workspace trust",
519            "trust-allowlisted",
520        ],
521    ) {
522        Some(DirectAnswerKind::AuthorizationPolicy)
523    } else if contains_any(
524        &lower,
525        &[
526            "tool classes",
527            "tool class",
528            "flat tool list",
529            "runtime tool classes",
530            "different runtime tool classes",
531        ],
532    ) || (lower.contains("repo reads")
533        && lower.contains("repo writes")
534        && contains_any(
535            &lower,
536            &[
537                "verification tools",
538                "git tools",
539                "external mcp tools",
540                "different runtime",
541            ],
542        ))
543    {
544        Some(DirectAnswerKind::ToolClasses)
545    } else if contains_any(
546        &lower,
547        &[
548            "built-in tool catalog",
549            "builtin tool catalog",
550            "builtin-tool dispatch",
551            "built-in tool dispatch",
552            "tool registry ownership",
553            "which file now owns",
554        ],
555    ) && contains_any(
556        &lower,
557        &[
558            "tool catalog",
559            "dispatch path",
560            "dispatch",
561            "tool registry",
562            "owns",
563        ],
564    ) {
565        Some(DirectAnswerKind::ToolRegistryOwnership)
566    } else if (lower.contains("other coding languages")
567        || lower.contains("what languages")
568        || lower.contains("know other languages"))
569        && contains_any(
570            &lower,
571            &[
572                "capable of making projects",
573                "can you make projects",
574                "can you build projects",
575            ],
576        )
577    {
578        Some(DirectAnswerKind::LanguageCapability)
579    } else if workflow_mode == WorkflowMode::Architect
580        && (lower.contains("session reset")
581            || (lower.contains("/clear") && lower.contains("/new") && lower.contains("/forget")))
582        && contains_any(&lower, &["redesign", "clearer", "easier", "understand"])
583    {
584        Some(DirectAnswerKind::ArchitectSessionResetPlan)
585    } else if toolchain_mode
586        && lower.contains("read-only")
587        && contains_any(
588            &lower,
589            &[
590                "tooling discipline",
591                "investigation plan",
592                "best read-only toolchain",
593                "tool choice discipline",
594                "what tools would you choose first",
595            ],
596        )
597    {
598        Some(DirectAnswerKind::Toolchain)
599    } else {
600        None
601    };
602
603    let primary_class = if direct_answer.is_some()
604        || mentions_stable_product_surface(&lower)
605        || mentions_product_truth_routing(&lower)
606    {
607        QueryIntentClass::ProductTruth
608    } else if architecture_overview_mode || preserve_project_map_output {
609        QueryIntentClass::RepoArchitecture
610    } else if toolchain_mode {
611        QueryIntentClass::Toolchain
612    } else if capability_mode {
613        QueryIntentClass::Capability
614    } else if mentions_runtime_trace || anti_guess || lower.contains("read-only") {
615        QueryIntentClass::RuntimeDiagnosis
616    } else if looks_like_mutation_request(user_input) {
617        QueryIntentClass::Implementation
618    } else {
619        QueryIntentClass::Unknown
620    };
621
622    QueryIntent {
623        primary_class,
624        direct_answer,
625        grounded_trace_mode: mentions_runtime_trace || lower.contains("read-only") || anti_guess,
626        capability_mode,
627        capability_needs_repo,
628        toolchain_mode,
629        host_inspection_mode,
630        preserve_project_map_output,
631        architecture_overview_mode,
632    }
633}
634
635pub(crate) fn is_capability_probe_tool(name: &str) -> bool {
636    matches!(
637        name,
638        "map_project"
639            | "read_file"
640            | "inspect_lines"
641            | "list_files"
642            | "grep_files"
643            | "lsp_definitions"
644            | "lsp_references"
645            | "lsp_hover"
646            | "lsp_search_symbol"
647            | "lsp_get_diagnostics"
648            | "trace_runtime_flow"
649            | "auto_pin_context"
650            | "list_pinned"
651    )
652}