Files
obsidian-vault/300 areas/350 AI Pi Coding Agent/AI tool to try and implement - MOUSE-1.md

6.3 KiB

title, type, status, created, updated, tags, modified
title type status created updated tags modified
AI tool to try and implement area active 2026-11-05 20:38 2026-09-26
ai
tool
dev
framework
implement
2026-11-05 20:38

AI tool to try and implement - MOUSE-1

Integrating Mouse-1 and Switch into an AI Engineering Stack

This document details how to integrate Mouse-1 (a low-latency, System 1 decision-routing model) alongside the Switch framework inside your Pi Dev (pi.dev) environment.


1. Architectural Overview

In a production AI agent or DevOps pipeline, offloading classification, routing, and safety filtering to a frontier reasoning model (like Claude or GPT) is slow and expensive.

Instead, use a tiered system:

  • System 2 (Slow/Complex): Large frontier models handle planning, code generation, and complex reasoning.
  • System 1 (Fast/Deterministic): Mouse-1 handles 100ms routing, tool gating, and error handling via fixed token probabilities.

[Agent State / Log Output]
│
▼
┌───────────────────────┐
│ osolmaz/switch │ <-- Evaluates variables, structures payload
└───────────┬───────────┘
│
▼
┌───────────────────────┐
│ Mouse-1 │ <-- Evaluates probability of choices in ~100ms
└───────────┬───────────┘
│
[Probabilities]
│
▼
┌───────────────────────┐
│ Pi Dev Extension │ <-- Blocks tool, executes rollback, or continues
└───────────────────────┘


2. Core Stack Components

Mouse-1 (Decision Model)

  • API Endpoint: https://mouse.dev
  • Pricing: Flat $0.10 per 1 million input tokens (outputs/probabilities are free).
  • Primary Benefit: Supports native LoRA fine-tuning via /v1/calibrations to realign routing decisions based on your unique system logs.

Switch (Dynamic Router)

  • Repository: osolmaz/switch
  • Primary Benefit: A declarative routing package that maps systemic states to execution branches. It prevents messy if/else logic when preparing schema choices for a decision layer.

3. Implementation in Pi Dev (pi.dev)

Pi Dev uses a TypeScript extension system to intercept and gate operations. The best way to implement Mouse-1 is at the tool lifecycle layer, intercepting tool calls or terminal inputs before execution.

File: extensions/mouse-guard.ts

import { Extension, ToolCallEvent } from '@earendil-works/pi-agent-core';

export const MouseGuardExtension: Extension = {
  name: 'mouse-guard',
  
  init(context) {
    // Intercept tool execution events before they hit the operating system
    context.events.on('tool:beforeCall', async (event: ToolCallEvent) => {
      
      // Bypass evaluation for safe read operations
      if (event.toolName === 'read_file') return;

      // Extract system context for the state string
      const state = `The agent is attempting to run tool: "${event.toolName}" with args: ${JSON.stringify(event.args)}. CWD State: ${context.projectSummary}`;
      
      // Define explicit, deterministic options
      const options = [
        "allow_execution",
        "block_and_request_human_review",
        "abort_with_error"
      ];

      try {
        const response = await fetch('https://mouse.dev', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${process.env.MOUSE_API_KEY}`
          },
          body: JSON.stringify({
            model: 'mouse-1',
            state: state,
            options: options
          })
        });

        const data = await response.json();
        const probabilities = data.probabilities;

        // Human-in-the-loop gate: Block if threshold for human review exceeds 70%
        if (probabilities.block_and_request_human_review > 0.70) {
          event.cancel("Execution blocked by Mouse-1: High probability of safety or structural risk.");
        }
      } catch (error) {
        // Fail-safe open if the API fails
        console.error("Mouse-1 decision layer unreachable. Defaulting to safe-open.", error);
      }
    });
  }
};

4. Using switch to Structure Python Pipeline Decisions

When orchestrating background DevOps flows (e.g., automated error recovery, CI/CD self-healing), use switch to map your application states directly to dynamic payloads.

File: devops_router.py

import requests
import os
from switch import Router

router = Router()

# Define explicit deployment branches
@router.case("run_helm_rollback")
def handle_rollback(log_context):
    print("Executing automatic helm rollback...")
    # os.system("helm rollback ...")

@router.case("trigger_slack_alert")
def handle_escalation(log_context):
    print("Escalating critical error to engineering Slack channel...")

# Orchestrate the Mouse-1 lookup layer
def evaluate_and_route(error_logs: str):
    # Dynamically extract potential options from our switch cases
    options = list(router.cases.keys())
    
    payload = {
        "model": "mouse-1",
        "state": f"CI/CD Pipeline Failure. Error Logs:\n{error_logs}",
        "options": options
    }
    
    headers = {
        "Authorization": f"Bearer {os.getenv('MOUSE_API_KEY')}",
        "Content-Type": "application/json"
    }
    
    # Query decision layer
    response = requests.post("https://mouse.dev", json=payload, headers=headers).json()
    probabilities = response["probabilities"]
    
    # Select the branch with the highest confidence
    best_choice = max(probabilities, key=probabilities.get)
    confidence = probabilities[best_choice]
    
    print(f"Mouse-1 selected branch: {best_choice} ({confidence*100:.1f}% confidence)")
    
    # Route execution via switch framework if confidence hits the SLA threshold
    if confidence > 0.85:
        router.route(best_choice, error_logs)
    else:
        router.route("trigger_slack_alert", error_logs)

if __name__ == "__main__":
    sample_crash = "Fatal Error: OOM Killed on pod replica-abc-123. Memory limit exceeded."
    evaluate_and_route(sample_crash)