|
| 1 | +# Context Overflow Contingency |
| 2 | + |
| 3 | +This feature provides a safety mechanism for subtasks that may encounter context overflow issues, particularly when using browser interactions or other tools that can return large amounts of content. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When enabled for a specific mode, the context overflow contingency feature monitors the token usage during subtask execution. If the context usage exceeds a configurable threshold (default: 90% of the model's context window), the subtask will automatically exit with an `attempt_completion` message, allowing the parent task to continue. |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +The feature is configured per-mode in the mode configuration file (`.roomodes` for project-specific or global settings): |
| 12 | + |
| 13 | +```yaml |
| 14 | +customModes: |
| 15 | + - slug: "mcp-expert" |
| 16 | + name: "MCP Expert" |
| 17 | + roleDefinition: "You are an expert at handling browser interactions using PlayWright" |
| 18 | + whenToUse: "Use this mode for browser automation tasks" |
| 19 | + groups: ["read", "edit", "browser", "command", "mcp"] |
| 20 | + contextOverflowContingency: |
| 21 | + enabled: true |
| 22 | + message: "Task failed because of a context overflow, possibly because webpage returned from the browser was too big" |
| 23 | + toolSpecific: |
| 24 | + browser_action: "Browser action returned too much content, causing context overflow" |
| 25 | + read_file: "File content was too large, causing context overflow" |
| 26 | +``` |
| 27 | +
|
| 28 | +### Configuration Options |
| 29 | +
|
| 30 | +- **`enabled`** (boolean): Whether to enable context overflow contingency for this mode |
| 31 | +- **`message`** (string, optional): Default message to use when context overflow is detected |
| 32 | +- **`toolSpecific`** (object, optional): Tool-specific messages that override the default message when a specific tool was the last one used |
| 33 | + |
| 34 | +## How It Works |
| 35 | + |
| 36 | +1. **Monitoring**: The feature checks context usage before making API requests and during task execution |
| 37 | +2. **Threshold Detection**: When context usage exceeds 90% of the model's context window, the contingency is triggered |
| 38 | +3. **Tool Detection**: The system attempts to identify the last tool used to provide more specific error messages |
| 39 | +4. **Graceful Exit**: The subtask exits with an `attempt_completion` containing the configured message |
| 40 | +5. **Parent Continuation**: The parent task receives the completion message and can continue execution |
| 41 | + |
| 42 | +## Use Cases |
| 43 | + |
| 44 | +This feature is particularly useful for: |
| 45 | + |
| 46 | +- **Browser Automation**: When web pages return large amounts of content |
| 47 | +- **File Processing**: When reading large files that exceed context limits |
| 48 | +- **API Interactions**: When external APIs return unexpectedly large responses |
| 49 | +- **Document Processing**: When processing large documents or datasets |
| 50 | + |
| 51 | +## Example Scenario |
| 52 | + |
| 53 | +Consider an "MCP Expert" mode that navigates to a webpage: |
| 54 | + |
| 55 | +1. The mode uses a browser tool to navigate to a page |
| 56 | +2. The webpage returns a very large HTML document |
| 57 | +3. Context usage jumps to 95% of the available window |
| 58 | +4. The contingency is triggered with the message: "Browser action returned too much content, causing context overflow" |
| 59 | +5. The subtask exits gracefully, and the parent task can handle the situation |
| 60 | + |
| 61 | +## Benefits |
| 62 | + |
| 63 | +- **Prevents Hanging**: Avoids situations where tasks get stuck due to context overflow |
| 64 | +- **Maintains Workflow**: Allows parent tasks to continue even when subtasks fail due to context issues |
| 65 | +- **Customizable Messages**: Provides clear, actionable feedback about why the task failed |
| 66 | +- **Tool-Specific Handling**: Different tools can have different failure messages for better debugging |
| 67 | + |
| 68 | +## Implementation Details |
| 69 | + |
| 70 | +The feature works by: |
| 71 | + |
| 72 | +1. Adding context overflow checks at key points in the task execution loop |
| 73 | +2. Monitoring token usage using the existing token counting infrastructure |
| 74 | +3. Comparing current usage against the model's context window limits |
| 75 | +4. Triggering graceful exit when thresholds are exceeded |
| 76 | +5. Using the existing `attempt_completion` mechanism for clean task termination |
| 77 | + |
| 78 | +## Limitations |
| 79 | + |
| 80 | +- Only works for subtasks (tasks with a parent task) |
| 81 | +- Requires the mode to have the feature explicitly enabled |
| 82 | +- Uses a fixed threshold of 90% context usage |
| 83 | +- Tool detection is based on pattern matching in recent messages |
0 commit comments