Skip to content

Commit a909b27

Browse files
committed
Fixes #4747: Add context overflow contingency for subtasks
- Add contextOverflowContingency configuration to ModeConfig schema - Implement context overflow detection in Task class - Add checkContextOverflowContingency method to monitor token usage - Support customizable failure messages (default and tool-specific) - Gracefully exit subtasks with attempt_completion when overflow detected - Add comprehensive tests for the new functionality - Add documentation explaining the feature and its usage This feature prevents subtasks from getting stuck due to context overflow, particularly useful for browser interactions and large content processing.
1 parent 2e2f83b commit a909b27

File tree

5 files changed

+1287
-0
lines changed

5 files changed

+1287
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

packages/types/src/mode.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export const groupEntrySchema = z.union([toolGroupsSchema, z.tuple([toolGroupsSc
3838

3939
export type GroupEntry = z.infer<typeof groupEntrySchema>
4040

41+
/**
42+
* ContextOverflowContingency
43+
*/
44+
45+
export const contextOverflowContingencySchema = z.object({
46+
enabled: z.boolean().default(false),
47+
message: z.string().optional(),
48+
toolSpecific: z.record(z.string(), z.string()).optional(), // tool name -> custom message
49+
})
50+
51+
export type ContextOverflowContingency = z.infer<typeof contextOverflowContingencySchema>
52+
4153
/**
4254
* ModeConfig
4355
*/
@@ -69,6 +81,7 @@ export const modeConfigSchema = z.object({
6981
customInstructions: z.string().optional(),
7082
groups: groupEntryArraySchema,
7183
source: z.enum(["global", "project"]).optional(),
84+
contextOverflowContingency: contextOverflowContingencySchema.optional(),
7285
})
7386

7487
export type ModeConfig = z.infer<typeof modeConfigSchema>

0 commit comments

Comments
 (0)