Implement oracle tool for complex reasoning

t-243·WorkTask·
·
·
·Omni/Agent/Tools.hs
Created3 months ago·Updated1 month ago

Description

Edit

Add an oracle tool that consults a reasoning model for planning, debugging, or architectural decisions.

Purpose

When jr encounters complex problems (multi-file refactors, debugging, architectural decisions), it should be able to "step back" and consult a more powerful reasoning model.

Data Types

data OracleArgs = OracleArgs
  { oracleTask :: Text           -- The question/problem to analyze
  , oracleContext :: Maybe Text  -- Optional context about current situation
  , oracleFiles :: Maybe [Text]  -- Optional file paths to include in context
  }
  deriving (Show, Eq, Generic)

Implementation

Step 1: Create the Oracle Tool

oracleTool :: Text -> Engine.Tool  -- Takes API key
oracleTool apiKey = Engine.Tool
  { Engine.toolName = "oracle",
    Engine.toolDescription = "Consult a reasoning model for complex planning, debugging, or architectural decisions. Use this when you need to think through a difficult problem.",
    Engine.toolJsonSchema = ...,
    Engine.toolExecute = executeOracle apiKey
  }

Step 2: Implement executeOracle

1. Parse the OracleArgs 2. If oracleFiles is provided, read each file and include in context 3. Build a prompt for the oracle: ` You are a senior engineering advisor. Analyze the following problem and provide specific, actionable guidance.

## Problem <task>

## Context <context>

## Relevant Files <file contents>

Provide your analysis and recommendations. Be specific about what to do and in what order. Do not implement - only advise. ` 4. Call OpenRouter API with model "anthropic/claude-sonnet-4" (or make it configurable) 5. Return the oracle response

Step 3: API Call

Use the same OpenRouter API pattern as in Engine.hs:

  • Endpoint: https://openrouter.ai/api/v1/chat/completions
  • Use the OPENROUTER_API_KEY from environment
  • Set max_tokens to 4000 (enough for detailed analysis)
  • Return the assistant message content

Tool Schema

{
  "type": "object",
  "properties": {
    "task": { "type": "string", "description": "The problem or question to analyze" },
    "context": { "type": "string", "description": "Optional context about current situation" },
    "files": { 
      "type": "array", 
      "items": { "type": "string" },
      "description": "Optional file paths to include in the analysis" 
    }
  },
  "required": ["task"]
}

Testing

Add a simple test that verifies the tool schema is valid. Full integration test would require API key.

Notes

  • The API key should be passed from Worker.hs (it already reads OPENROUTER_API_KEY)
  • Consider adding a cost limit for oracle calls (separate from main agent budget)
  • The oracle should NOT have access to tools - it only advises, does not act
  • Add oracleTool to exports and allTools list

Timeline (4)

🔄[human]Open → InProgress1 month ago
💬[human]1 month ago

CHANGES REQUESTED: Looking at this task implementation, let me evaluate the code changes against the requirements.

Task Requirements Checklist

1. Create the Oracle Tool

  • oracleTool function takes API key and returns Engine.Tool
  • Has correct name, description, and JSON schema
  • Properly exported

2. Data Types

  • OracleArgs with oracleTask, oracleContext, oracleFiles
  • Proper FromJSON instance

3. Implementation

  • Parses OracleArgs correctly
  • Reads files if oracleFiles is provided (with truncation for large files)
  • Builds a prompt matching the spec
  • Calls OpenRouter API with correct endpoint and model
  • Returns oracle response

4. Tool Schema

  • Matches the spec exactly

5. Testing

  • Tests for schema validity
  • Tests for argument parsing
  • Tests for allToolsWithOracle
  • Tests for buildOraclePrompt

6. Exports

  • oracleTool, allToolsWithOracle, OracleArgs all exported

Build/Test Status

  • Build: ✅ Passed
  • Tests: ✅ All 31 tests passed

Lint Issues

There are lint warnings: 1. File has 1248 lines (max 1000) - this is a pre-existing issue, not introduced by this change 2. Missing shebang - also pre-existing 3. hlint suggestions: Two data types should be newtype instead of data:

  • OracleApiResponse
  • OracleChoice

These hlint suggestions are valid and easy fixes. The code uses data for single-field types that should be newtype for efficiency.

Code Quality

  • Follows project conventions (imports, structure)
  • Good error handling
  • File content truncation is a nice touch
  • Reasonable separation of concerns with helper functions

Issues Found

1. Lint failure: hlint found 2 issues that should be fixed (datanewtype for single-field types)

The implementation is correct and complete, but the hlint warnings should be addressed since they represent a lint failure.

🔄[human]InProgress → Done1 month ago