|
| 1 | +# Implementation Plan: Tier Protocol Model Routing System |
| 2 | + |
| 3 | +This plan outlines the implementation of the "Tier Protocol" for the Gemini CLI, enabling dynamic model routing based on task complexity (Orchestration, Thinking, Execution). |
| 4 | + |
| 5 | +## Objective |
| 6 | +Establish a three-tier model routing system to optimize performance, cost, and reasoning quality by using specialized models for different phases of the agent's workflow. |
| 7 | + |
| 8 | +## Architectural Impact |
| 9 | +- **Middleware Integration**: Adds a `BeforeModel` hook (`tier_router.py`) that acts as a traffic controller for model selection. |
| 10 | +- **Semantic Signaling**: Uses explicit `[Tier: *]` tags in assistant messages to communicate routing intent to the framework. |
| 11 | +- **Centralized Configuration**: Standardizes model mapping and default settings in `.gemini/settings.json`. |
| 12 | +- **System-level Feedback**: Integrates `notify-send` for real-time user visibility of tier switches. |
| 13 | + |
| 14 | +## File Operations |
| 15 | + |
| 16 | +| Action | File | Description | |
| 17 | +| :--- | :--- | :--- | |
| 18 | +| **Modify** | `GEMINI.md` | Define the Tier Protocol, models, signals, and usage guidelines. | |
| 19 | +| **Create** | `.gemini/hooks/tier_router.py` | Implement the logic to parse signals and route models. | |
| 20 | +| **Modify** | `.gemini/settings.json` | Register the `BeforeModel` hook and set the default model. | |
| 21 | +| **Modify** | `.gemini/commands/*.toml` | Update command prompts to include tier signaling instructions. | |
| 22 | + |
| 23 | +## Step-by-Step Execution |
| 24 | + |
| 25 | +### Step 1: Document the Tier Protocol in `GEMINI.md` |
| 26 | +Add a new section to `GEMINI.md` defining the protocol: |
| 27 | + |
| 28 | +```markdown |
| 29 | +## Tier Protocol |
| 30 | + |
| 31 | +To optimize reasoning and execution, this project uses a multi-tier model routing system: |
| 32 | + |
| 33 | +| Tier | Model | Use Case | Signal | |
| 34 | +| :--- | :--- | :--- | :--- | |
| 35 | +| **Orchestrator** | `gemini-3-flash-preview` | Default routing, task management, and fast orchestration. | `[Tier: Orchestrator]` | |
| 36 | +| **Thinker** | `gemini-3.1-pro-preview` | Deep architectural planning, RCA, and complex logic. | `[Tier: Thinker]` | |
| 37 | +| **Executioner** | `gemini-2.5-flash` | High-velocity coding, boilerplate, and tool-heavy tasks. | `[Tier: Executioner]` | |
| 38 | + |
| 39 | +**Guidelines:** |
| 40 | +- **Implicit Default:** All sessions start in Orchestrator tier. |
| 41 | +- **Explicit Switching:** Always emit the signal (e.g., `[Tier: Thinker]`) at the end of your response to route the *next* turn to that model. |
| 42 | +- **Phase Transition:** Switch to **Thinker** for discovery/planning and **Executioner** for task implementation. |
| 43 | +``` |
| 44 | + |
| 45 | +### Step 2: Implement the `tier_router` Hook |
| 46 | +Create `.gemini/hooks/tier_router.py` with the following logic: |
| 47 | + |
| 48 | +```python |
| 49 | +import sys |
| 50 | +import json |
| 51 | +import re |
| 52 | +import os |
| 53 | +import subprocess |
| 54 | + |
| 55 | +# Model Mapping |
| 56 | +TIER_MAPPING = { |
| 57 | + "Orchestrator": "gemini-3-flash-preview", |
| 58 | + "Thinker": "gemini-3.1-pro-preview", |
| 59 | + "Executioner": "gemini-2.5-flash" |
| 60 | +} |
| 61 | + |
| 62 | +def main(): |
| 63 | + try: |
| 64 | + input_data = sys.stdin.read() |
| 65 | + if not input_data: |
| 66 | + print(json.dumps({"decision": "allow"})) |
| 67 | + return |
| 68 | + |
| 69 | + data = json.loads(input_data) |
| 70 | + history = data.get("history", []) |
| 71 | + |
| 72 | + # Identify target tier from last assistant message |
| 73 | + target_tier = None |
| 74 | + for message in reversed(history): |
| 75 | + if message.get("role") == "assistant": |
| 76 | + content = message.get("content", "") |
| 77 | + match = re.search(r"\[Tier:\s*(Orchestrator|Thinker|Executioner)\]", content) |
| 78 | + if match: |
| 79 | + target_tier = match.group(1) |
| 80 | + break |
| 81 | + |
| 82 | + # Prepare response |
| 83 | + response = {"decision": "allow"} |
| 84 | + |
| 85 | + if target_tier and target_tier in TIER_MAPPING: |
| 86 | + model_id = TIER_MAPPING[target_tier] |
| 87 | + response["model"] = model_id |
| 88 | + response["systemMessage"] = f"Tier Protocol: Active ({target_tier})" |
| 89 | + |
| 90 | + # User Notification |
| 91 | + try: |
| 92 | + subprocess.run([ |
| 93 | + "notify-send", |
| 94 | + "Gemini Tier Switch", |
| 95 | + f"Routing to {target_tier}: {model_id}", |
| 96 | + "-i", "dialog-information", "-t", "2000" |
| 97 | + ], check=False) |
| 98 | + except: |
| 99 | + pass |
| 100 | + |
| 101 | + print(json.dumps(response)) |
| 102 | + |
| 103 | + except Exception as e: |
| 104 | + sys.stderr.write(f"Error in tier_router: {str(e)}\n") |
| 105 | + print(json.dumps({"decision": "allow"})) |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
| 109 | +``` |
| 110 | + |
| 111 | +### Step 3: Update `settings.json` Configuration |
| 112 | +Register the hook and set the default model: |
| 113 | + |
| 114 | +```json |
| 115 | +{ |
| 116 | + "model": "gemini-3-flash-preview", |
| 117 | + "hooks": { |
| 118 | + "BeforeModel": [ |
| 119 | + { |
| 120 | + "matcher": "*", |
| 121 | + "hooks": [ |
| 122 | + { |
| 123 | + "name": "tier-router", |
| 124 | + "type": "command", |
| 125 | + "command": "python3 .gemini/hooks/tier_router.py" |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Step 4: Update Command Prompts for Signaling |
| 135 | +Update `.gemini/commands/*.toml` to incorporate tier instructions. |
| 136 | + |
| 137 | +#### Thinker-Heavy Commands |
| 138 | +Add `[Tier: Thinker]` requirement to: |
| 139 | +- `brainstorm.toml`: "Start by switching to `[Tier: Thinker]` to engage deep reasoning." |
| 140 | +- `plan.toml`: "Use `[Tier: Thinker]` for architectural analysis phases." |
| 141 | +- `research.toml`, `review.toml`, `learn.toml`, `issues.toml`, `document.toml`, `draft.toml`: Update to prioritize Thinker. |
| 142 | + |
| 143 | +#### Executioner-Heavy Commands |
| 144 | +Add `[Tier: Executioner]` requirement to: |
| 145 | +- `commit.toml`: "Switch to `[Tier: Executioner]` for rapid change grouping and committing." |
| 146 | +- `scaffold.toml`: "Use `[Tier: Executioner]` for boilerplate generation." |
| 147 | + |
| 148 | +#### Hybrid Commands (Phase-based Routing) |
| 149 | +- `debug.toml`: |
| 150 | + - **Phase 2 (Hypothesis):** Switch to `[Tier: Thinker]`. |
| 151 | + - **Phase 3 (Testing):** Switch to `[Tier: Executioner]` for diagnostic branch work. |
| 152 | +- `task.toml`: |
| 153 | + - **Phase 3 (TCR Loop):** Instruct the Orchestrator to switch to `[Tier: Executioner]` for implementation steps. |
| 154 | + |
| 155 | +## Testing Strategy |
| 156 | +1. **Routing Logic Validation**: Use a mock session JSON (containing `[Tier: Thinker]`) and pipe it into `tier_router.py` to verify the output contains `"model": "gemini-3.1-pro-preview"`. |
| 157 | +2. **Notification Test**: Trigger the hook and verify the `notify-send` desktop alert appears. |
| 158 | +3. **End-to-End Command Test**: Run `/plan` and verify the first assistant turn ends with `[Tier: Thinker]`, followed by the model switch in the subsequent turn. |
| 159 | +4. **Integration Test**: Verify `settings.json` registration doesn't interfere with existing hooks (e.g., `log.py`, `notify.py`). |
0 commit comments