tests — reasoning

Module: tests-reasoning Cohesion: 0.80 Members: 0

tests — reasoning

The reasoning module provides sophisticated problem-solving capabilities to the agent, leveraging various AI reasoning techniques like Chain-of-Thought (CoT) and Tree-of-Thought (ToT)/MCTS. It offers both automated complexity detection and user-controlled modes to guide the agent's thinking process.

This module is composed of three main parts:

  1. ReasoningFacade: The core interface for executing different reasoning strategies and managing their lifecycle.
  2. ReasoningMiddleware: Integrates reasoning capabilities into the agent's message processing pipeline by detecting problem complexity and injecting guidance.
  3. Think Command Handlers: Provides user-facing commands (/think) to control reasoning modes and initiate problem-solving.

Core Concepts

The module supports several reasoning modes, each offering a different trade-off between computational cost, latency, and problem-solving depth:

Beyond explicit mode selection, the module also features:

Architecture Overview

The following diagram illustrates how the different components of the reasoning module interact:

graph TD
    subgraph User Interaction
        User -->|/think command| ThinkHandlers
    end

    subgraph Agent Core
        AgentLoop -->|User Message| ReasoningMiddleware
    end

    subgraph Reasoning Logic
        ThinkHandlers -->|setActiveThinkingMode| GlobalThinkingMode
        ReasoningMiddleware -->|getActiveThinkingMode| GlobalThinkingMode
        ReasoningMiddleware -->|detectComplexity| ReasoningMiddleware
        ReasoningMiddleware -->|injects <reasoning_guidance>| LLMInput
        LLMInput --> LLM(LLM Model)

        ThinkHandlers -->|calls solve()| ReasoningFacade
        ReasoningFacade -->|dispatches to| TreeOfThoughtReasoner
        ReasoningFacade -->|dispatches to| ChainOfThoughtReasoner
        ReasoningFacade -->|tracks usage| UsageStats
    end

Components

ReasoningFacade (src/agent/reasoning/reasoning-facade.ts)

The ReasoningFacade acts as the primary interface for interacting with the underlying reasoning engines (Chain-of-Thought, Tree-of-Thought). It abstracts away the specifics of different reasoning algorithms, providing a unified solve() method. It also handles API key management, usage tracking, result formatting, and the heuristics for auto-selecting reasoning modes and auto-escalation.

Key Responsibilities:

ReasoningMiddleware (src/agent/middleware/reasoning-middleware.ts)

The ReasoningMiddleware integrates the reasoning capabilities into the agent's message processing pipeline. Its primary role is to detect the complexity of user prompts and, if appropriate, inject a system message () to encourage the LLM to engage in more structured or advanced reasoning.

Key Responsibilities:

Think Command Handlers (src/commands/handlers/think-handlers.ts)

The think-handlers module provides user-facing commands (/think) to control the agent's reasoning behavior. Users can manually set reasoning modes, check status, and directly initiate reasoning for specific problems.

Key Responsibilities:

How it Works

User-Initiated Reasoning via /think

  1. A user types a command like /think deep "How do I refactor this complex module?".
  2. The handleThink function in think-handlers.ts parses the command.
  3. It calls setActiveThinkingMode('deep') to update the global reasoning mode.
  4. It then constructs a Problem object from the problem description and calls ReasoningFacade.solve() with the problem and the specified mode.
  5. ReasoningFacade.solve() executes the appropriate reasoning strategy (in this case, TreeOfThoughtReasoner for deep mode).
  6. The raw result from the reasoner is then formatted by ReasoningFacade.formatResult() into a human-readable string.
  7. handleThink returns this formatted result, which is displayed to the user.

Agent-Initiated Reasoning via ReasoningMiddleware

  1. A user sends a message to the agent, e.g., "Please design a robust, scalable architecture for a new microservice, considering trade-offs between latency and cost."
  2. As part of the agent's message processing loop, ReasoningMiddleware.beforeTurn() is invoked.
  3. The middleware first checks getActiveThinkingMode().

  1. Based on the detected complexity or explicit mode, the middleware injects a system message containing into the message history. This guidance is a prompt engineering technique designed to steer the LLM towards using its internal reasoning capabilities more effectively.
  2. The LLM receives the augmented message history, including the reasoning guidance. It is then expected to perform the reasoning based on this guidance, potentially leading to a more structured and higher-quality response.

Configuration and Usage

API Key

All reasoning operations that involve external LLM calls (i.e., anything beyond local complexity detection) require an API key. This is typically configured via an environment variable:

export GROK_API_KEY="your_grok_api_key_here"

/think Command

The /think command provides direct control over the agent's reasoning behavior:

ReasoningMiddleware Auto-Detection

The automatic complexity detection and guidance injection by ReasoningMiddleware can be toggled programmatically:

import { createReasoningMiddleware } from &#39;../../src/agent/middleware/reasoning-middleware.js&#39;;

const middleware = createReasoningMiddleware(); class="hl-cmt">// Auto-detect is true by default
middleware.setAutoDetect(false); class="hl-cmt">// Disable auto-detection
class="hl-cmt">// ... later ...
middleware.setAutoDetect(true); class="hl-cmt">// Re-enable auto-detection