Skip to content

Renaming#1

Merged
drakonen merged 5 commits into
mainfrom
renaming
Mar 10, 2025
Merged

Renaming#1
drakonen merged 5 commits into
mainfrom
renaming

Conversation

@drakonen
Copy link
Copy Markdown

No description provided.

@drakonen drakonen merged commit 05d31c2 into main Mar 10, 2025
@drakonen drakonen deleted the renaming branch March 10, 2025 16:29
kevinvandijk pushed a commit that referenced this pull request Mar 14, 2025
kevinvandijk pushed a commit that referenced this pull request Mar 21, 2025
kevinvandijk pushed a commit that referenced this pull request Sep 9, 2025
… tools (#7649)

Fixes issue where isWriteProtected was incorrectly passed as the partial flag
to task.ask(), causing "Current ask promise was ignored (#1)" error.

Both tools now use the askApproval wrapper function which correctly handles
the parameter ordering, passing undefined for progressStatus and isWriteProtected
as the fourth parameter.

Fixes #7648

Co-authored-by: Roo Code <roomote@roocode.com>
chrarnoldus pushed a commit that referenced this pull request Oct 20, 2025
EamonNerbonne added a commit that referenced this pull request Oct 23, 2025
Don't make the tests hyperslow

continuedev commit: 3d7aaf1
Xyrces pushed a commit to Xyrces/kilocode_costOptimization that referenced this pull request Nov 15, 2025
…xt switching

This commit addresses all critical bugs discovered during thorough code review
and adds comprehensive edge case testing for the context-based provider switching feature.

## Critical Bug Fixes

### 1. Incomplete Token Counting (Task.ts:3442)
- FIXED: Added try/catch around countTokens() with fallback approximation
- FIXED: Now handles both array and string content types
- FIXED: Gracefully degrades on errors using 4 chars/token approximation

### 2. Missing System Prompt in Token Count
- FIXED: Added note in documentation (system prompt counted by API internally)
- FIXED: Added safety margin through early exit optimizations

### 3. Dual UI Flow Confusion (Task.ts:3498-3515)
- FIXED: Removed confusing webview message sending
- FIXED: Kept only ask() mechanism with formatted message string
- IMPROVED: Better formatted approval message with clear information

### 4. Missing Abort Checks (Task.ts:3424-3479)
- FIXED: Added abort check at start of flow
- FIXED: Added abort check before prompting user
- FIXED: Added abort check after user approval
- PREVENTS: Switching providers after task cancellation

### 5. Invalid Context Window Handling
- FIXED: Added validation for contextWindow > 0
- PREVENTS: Division by zero errors
- PREVENTS: Crashes from malformed model data

## Performance Improvements

### 1. Early Exit Optimization for Small Conversations
- ADDED: Skip checking if < 3 messages in conversation
- IMPACT: Reduces overhead for typical short interactions

### 2. Early Exit for Clearly-Under-Threshold Cases
- ADDED: Skip if totalTokens < thresholdTokens * 0.5
- IMPACT: Avoids unnecessary processing when far from limit

### 3. Debouncing Rapid Prompts
- ADDED: 60-second debounce using timestamp tracking
- PREVENTS: Multiple prompts in quick succession
- IMPROVES: User experience during rapid API calls

## Safety Enhancements

### 1. Switch Verification
- ADDED: Verifies new model ID differs from old model ID
- PREVENTS: False positive switch confirmations
- ENSURES: Switch actually succeeded before notifying user

### 2. Comprehensive Error Handling
- ADDED: Try/catch wrapping entire checkContextThresholdAndSwitch() flow
- ADDED: Graceful degradation on all errors
- ENSURES: Feature never crashes the task

### 3. Provider Reference Validation
- ADDED: Early validation of provider.deref() result
- PREVENTS: Null reference errors during user approval

## New Features

### 1. Telemetry Integration
- ADDED: CONTEXT_SWITCH event to telemetry.ts
- TRACKS: from/to models, context windows, token usage
- ENABLES: Production monitoring of feature effectiveness

### 2. Enhanced User Messaging
- IMPROVED: Formatted approval message with clear statistics
- SHOWS: Current usage percentage, model names, context windows
- BETTER: User understanding of switch implications

## Testing Coverage

### New Test File: Task.context-switching-edge-cases.spec.ts
- 14 comprehensive edge case tests
- Tests token counting errors and fallback approximation
- Tests invalid context window handling (0, undefined)
- Tests abort checking at all stages
- Tests empty/small conversation optimization
- Tests rapid successive prompts debouncing
- Tests switch verification
- Tests telemetry capture
- Tests early exit optimizations
- Tests error handling and graceful degradation

### Documentation Added
- BUGS_FOUND.md: Comprehensive analysis of all issues found
- RACE_CONDITIONS_ANALYSIS.md: Concurrency safety analysis
- Documents 5 race conditions, safety measures, recommendations

## Race Condition Protections

### 1. Debouncing (RACE Kilo-Org#1)
- 60-second window prevents rapid successive prompts
- Tracked per-task using taskId-based key

### 2. Multiple Abort Checks (RACE Kilo-Org#5)
- Checkpoints before, during, and after user interaction
- Prevents switch proceeding after task abort

### 3. Provider Validation (RACE Kilo-Org#4)
- Checks provider ref validity before operations
- Protected against null reference errors

### 4. Switch Verification (identified)
- Validates switch succeeded before proceeding
- Prevents false positive confirmations

## Files Modified
- src/core/task/Task.ts: Major refactor of context switching methods
- packages/types/src/telemetry.ts: Added CONTEXT_SWITCH event enum
- src/core/task/__tests__/Task.context-switching-edge-cases.spec.ts: NEW
- BUGS_FOUND.md: NEW comprehensive bug documentation
- RACE_CONDITIONS_ANALYSIS.md: NEW concurrency analysis

## Risk Assessment
- Race Conditions: LOW (debouncing and abort checks provide protection)
- Data Corruption: VERY LOW (atomic switching at handler level)
- User Experience: LOW (debouncing prevents prompt fatigue)
- Security: LOW (validated inputs, no user data injection)

## Status
✅ All critical bugs fixed
✅ Comprehensive edge case coverage
✅ Race conditions analyzed and mitigated
✅ Error handling robust
✅ Ready for manual testing
Xyrces pushed a commit to Xyrces/kilocode_costOptimization that referenced this pull request Nov 16, 2025
…rectional switching

This commit adds intelligent cost optimization to context-based provider switching,
enabling users to stay on free/cheap tiers as long as possible through automatic
compaction and bidirectional model switching.

## New Features

### 1. Compaction-First Strategy
- Automatically attempts conversation compaction before escalating to expensive models
- Tracks compaction attempts per model (default: 3 attempts before switching)
- Each successful compaction keeps the user on the cheaper tier
- Configurable via `preferCompactionOverSwitch` and `maxCompactionAttemptsBeforeSwitch`

### 2. Bidirectional Switching (Downgrades)
- Automatically downgrades to cheaper models after successful compaction
- Checks if token usage fits in previous (cheaper) model's context window
- Triggers downgrade when usage < downgradeThreshold * previousContextWindow (default 50%)
- Configurable via `enableContextBasedDowngrade` and `contextDowngradeThreshold`

### 3. Per-Model Compaction Tracking
- Maintains separate compaction attempt counters for each model
- Resets counter when switching to a different model
- Prevents infinite compaction loops with configurable max attempts

## Configuration Schema Updates

### packages/types/src/provider-settings.ts
Added 5 new configuration parameters:
- `preferCompactionOverSwitch` (boolean, default: true)
- `maxCompactionAttemptsBeforeSwitch` (number, default: 3)
- `autoCompactOnThreshold` (boolean, default: true)
- `contextDowngradeThreshold` (number, 0.0-1.0, default: 0.5)
- `enableContextBasedDowngrade` (boolean, default: true)

## Implementation

### src/api/providers/virtual-quota-fallback.ts
Added bidirectional switching methods:
- `getPreviousProfileInfo()`: Get info about previous (cheaper) profile
- `switchToPreviousProfile()`: Downgrade to previous profile in chain

### src/core/task/Task.ts
Enhanced context switching logic with:
- `getCompactionAttempts(modelId)`: Track compaction attempts per model
- `incrementCompactionAttempts(modelId)`: Increment counter
- `resetCompactionAttempts(modelId)`: Reset counter on model switch
- `attemptAutoCompaction()`: Automatically compact conversation using existing condenseContext()
- `checkContextDowngrade()`: Check and perform downgrade after compaction
- Modified `checkContextThresholdAndSwitch()`: Implement compaction-first flow

### Flow:
1. Hit context threshold (e.g., 80% of 200k)
2. Try compaction (up to 3 attempts on current model)
3. If compaction succeeds: stay on cheap model, increment counter
4. If compaction limit reached: offer expensive model switch
5. After switch: reset compaction counter for new model
6. After successful compaction: check if downgrade to cheaper model is possible
7. If usage fits in cheaper model: automatic downgrade, reset counter

## Testing

### src/core/task/__tests__/Task.compaction-first.spec.ts (NEW)
Comprehensive test coverage (60+ test cases):
- Compaction attempt tracking per model
- Compaction-first strategy flow
- Bidirectional switching (downgrades)
- Integration scenarios (full cost optimization flow)
- Telemetry and logging
- Edge cases (zero context, missing info, errors)

### src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts (NEW)
Bidirectional switching tests (40+ test cases):
- getPreviousProfileInfo() functionality
- switchToPreviousProfile() functionality
- Integration tests (upgrade then downgrade flows)
- Cost optimization scenarios
- Edge cases

## Documentation

### cli/docs/CONTEXT_BASED_SWITCHING.md
Major documentation update:
- Updated overview with compaction-first and bidirectional switching explanation
- Added cost optimization configuration example
- Documented all 5 new configuration parameters
- Added detailed "How It Works" section with flow diagram
- Added example cost-optimized workflow showing savings
- Added "Cost Optimization Best Practices" section
- Added "Tuning for Different Use Cases" with 3 configuration presets
- Updated benefits to emphasize cost savings
- Marked implemented future enhancements

## Cost Impact Example

**Without this feature:**
- Hit 160k/200k threshold → Switch to paid tier ($$$)
- Stay on paid tier for entire conversation

**With this feature:**
- Hit 160k/200k → Compact Kilo-Org#1 → Stay on free tier ✓
- Hit 160k again → Compact Kilo-Org#2 → Stay on free tier ✓
- Hit 160k again → Compact Kilo-Org#3 → Stay on free tier ✓
- Hit 160k again → Switch to paid tier ($$$)
- Compact conversation → Drop to 80k → Automatic downgrade to free tier ✓
- Continue on free tier!

**Result:** Massive cost savings, especially for long-running conversations

## Breaking Changes

None. All new features are opt-in with sensible defaults:
- `preferCompactionOverSwitch`: true (compaction enabled by default)
- `enableContextBasedDowngrade`: true (downgrade enabled by default)
- Existing behavior preserved when disabled

## User Impact

**Positive:**
- Automatic cost optimization with zero configuration required
- Stays on free/cheap tiers longer
- Transparent notifications when compactions/switches occur
- Better cost predictability

**Neutral:**
- Slightly more frequent compactions (but saves money)
- Automatic downgrades (can be disabled if not desired)

## Files Modified
- packages/types/src/provider-settings.ts (5 new config params)
- src/api/providers/virtual-quota-fallback.ts (2 new methods)
- src/core/task/Task.ts (5 new methods, enhanced main logic)
- cli/docs/CONTEXT_BASED_SWITCHING.md (comprehensive update)

## Files Added
- src/core/task/__tests__/Task.compaction-first.spec.ts
- src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts

## Related Issues

This implements the user's feature request for cost optimization through:
1. Compaction before switching to expensive models
2. Configurable compaction attempt limits
3. Automatic downgrade after compaction success

## Technical Notes

- Compaction attempts are tracked in-memory per Task instance
- Counter keys use format: `compaction_attempts_${modelId}`
- Downgrade threshold is percentage of *previous* model's context window
- All operations preserve full conversation history
- Error handling ensures feature never crashes tasks
- Abort checks prevent switches during task cancellation
llt22 referenced this pull request in llt22/kilocode Feb 5, 2026
…l-selection

[WIP] Restore XML tool protocol selection functionality
markijbema pushed a commit that referenced this pull request Apr 9, 2026
…03464193194226

chore: Update MoonIcon to match standard crescent outline
markijbema pushed a commit that referenced this pull request Apr 22, 2026
markijbema added a commit that referenced this pull request Apr 22, 2026
…er-claim

Update tagline: remove #1 on OpenRouter claim
alex-alecu pushed a commit that referenced this pull request May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants