fix: add prompt size safeguard to prevent Copilot context overflow#34
fix: add prompt size safeguard to prevent Copilot context overflow#34mohamedsedkiyouzbechi wants to merge 1 commit intomainfrom
Conversation
WalkthroughThis PR adds a prompt size safeguard feature that validates active prompts during sync operations by checking total prompt size, active prompt count, and repository count against configured thresholds, displaying a warning notification with actionable buttons when limits are exceeded. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant SyncManager
participant FileSystem as File System
participant NotificationManager
participant UI as User Interface
User->>SyncManager: Trigger sync
SyncManager->>SyncManager: Clean up symlinks
SyncManager->>SyncManager: Recreate active prompt symlinks
SyncManager->>SyncManager: Call validatePromptSize()
SyncManager->>FileSystem: Read prompt files & calculate sizes
FileSystem-->>SyncManager: File sizes & metadata
SyncManager->>SyncManager: Check against thresholds
alt Thresholds exceeded
SyncManager->>NotificationManager: showPromptSizeWarning(details)
NotificationManager->>UI: Display warning with buttons
UI-->>User: Show "Manage Prompts" / "Open Settings" / "Dismiss"
User->>UI: Select action
UI->>UI: Focus prompts UI or open settings
else Within limits
SyncManager->>SyncManager: Complete sync
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip Migrating from UI to YAML configuration.Use the |
There was a problem hiding this comment.
Pull request overview
Adds a post-sync safeguard in Promptitude to help prevent GitHub Copilot context overflow by cleaning up inactive prompts from the active prompts directory and warning users when active prompts exceed configured safety thresholds.
Changes:
- Introduces prompt/repo threshold constants for warning triggers.
- Adds an always-visible warning notification with actions to help users remediate oversized prompt sets.
- Hooks inactive prompt cleanup and a new prompt-size validation step into the sync lifecycle.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/constant.ts |
Defines size/count/repo thresholds used by the safeguard. |
src/utils/notifications.ts |
Adds a dedicated warning notification UI with action buttons. |
src/syncManager.ts |
Runs inactive prompt cleanup and validates active prompt size/count after sync. |
CHANGELOG.md |
Documents the new safeguard behavior under [Unreleased]. |
You can also share your feedback on Copilot code review. Take the survey.
| ); | ||
|
|
||
| if (result === 'Manage Prompts') { | ||
| vscode.commands.executeCommand('promptitude-prompts.focus'); |
| // Remove disabled prompts from the active prompts directory | ||
| await this.cleanupInactivePrompts(); | ||
|
|
| const fs = require('fs').promises; | ||
| const promptsDir = this.config.getPromptsDirectory(); | ||
|
|
||
| // Ensure the prompts directory exists | ||
| await this.fileSystem.ensureDirectoryExists(promptsDir); | ||
|
|
||
| const entries = await this.fileSystem.readDirectory(promptsDir); | ||
| const promptFiles = entries.filter(f => this.isPromptFile(f)); | ||
|
|
||
| // Calculate total size of active prompt files | ||
| let totalSize = 0; | ||
| let activeCount = 0; | ||
|
|
||
| for (const file of promptFiles) { | ||
| try { | ||
| const filePath = this.fileSystem.joinPath(promptsDir, file); | ||
| const stats = await fs.stat(filePath); | ||
| totalSize += stats.size; |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/syncManager.ts (1)
1235-1293: Well-structured validation method with appropriate error handling.The implementation correctly:
- Measures actual content size (fs.stat follows symlinks)
- Validates after cleanup operations complete
- Logs appropriately and doesn't block sync on validation failure
One minor suggestion: the empty catch block at lines 1260-1262 silently skips files. Consider adding a debug log for troubleshooting.
💡 Optional: Add debug logging for skipped files
} catch { - // Skip files that can't be stat'd + // Skip files that can't be stat'd (e.g., broken symlinks) + this.logger.debug(`Skipped file during size validation: ${file}`); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/syncManager.ts` around lines 1235 - 1293, The inner catch in validatePromptSize silently skips files that can't be stat'd; update that catch to log a debug message including the file name/path and the caught error using this.logger.debug (or this.logger.warn if preferred) so failures in fs.stat are visible for troubleshooting—locate the loop over promptFiles in validatePromptSize, find the try { const stats = await fs.stat(filePath); ... } catch { ... } and replace the empty catch with a log that references filePath and the caught error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/utils/notifications.ts`:
- Around line 165-187: In showPromptSizeWarning, the invoked command ID
'promptitude-prompts.focus' doesn't exist and will throw at runtime; replace it
with the correct view-focus command matching the view ID in package.json (likely
'promptitude.cards.focus') when calling
vscode.commands.executeCommand('promptitude-prompts.focus'), or if the intent
was to focus the Prompt Details view use 'promptitude.details.focus' instead;
update the executeCommand invocation(s) in showPromptSizeWarning to use the
exact view ID(s) (promptitude.cards.focus or promptitude.details.focus) so the
command resolves successfully.
---
Nitpick comments:
In `@src/syncManager.ts`:
- Around line 1235-1293: The inner catch in validatePromptSize silently skips
files that can't be stat'd; update that catch to log a debug message including
the file name/path and the caught error using this.logger.debug (or
this.logger.warn if preferred) so failures in fs.stat are visible for
troubleshooting—locate the loop over promptFiles in validatePromptSize, find the
try { const stats = await fs.stat(filePath); ... } catch { ... } and replace the
empty catch with a log that references filePath and the caught error.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1aa043df-5323-411d-8d74-6a77780912fb
📒 Files selected for processing (4)
CHANGELOG.mdsrc/constant.tssrc/syncManager.tssrc/utils/notifications.ts
|
|
||
| /** | ||
| * Show a warning when the total prompt size or count exceeds safe thresholds. | ||
| * Always shown regardless of showNotifications setting to prevent Copilot context overflow. | ||
| */ | ||
| async showPromptSizeWarning(details: { totalSizeKB: number; activeCount: number; repoCount: number; reasons: string[] }): Promise<void> { | ||
| const reasonText = details.reasons.join('; '); | ||
| const message = `⚠️ Promptitude: Your active prompts may be too large for GitHub Copilot (${details.totalSizeKB.toFixed(0)} KB across ${details.activeCount} prompts from ${details.repoCount} repos). ${reasonText}. Consider disabling unused prompts, reducing synced repositories, or switching to manual sync.`; | ||
|
|
||
| // Always show this warning regardless of notification settings — it's a safeguard | ||
| const result = await vscode.window.showWarningMessage( | ||
| message, | ||
| 'Manage Prompts', | ||
| 'Open Settings', | ||
| 'Dismiss' | ||
| ); | ||
|
|
||
| if (result === 'Manage Prompts') { | ||
| vscode.commands.executeCommand('promptitude-prompts.focus'); | ||
| } else if (result === 'Open Settings') { | ||
| vscode.commands.executeCommand('workbench.action.openSettings', 'promptitude'); | ||
| } | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Search for the command registration to ensure it exists
rg -n "promptitude-prompts" --type=ts -C2Repository: nventive/Promptitude
Length of output: 476
🏁 Script executed:
rg -n "registerCommand.*promptitude-prompts" --type=tsRepository: nventive/Promptitude
Length of output: 46
🏁 Script executed:
fd -name "extension.ts" -o -name "activate.ts" -o -name "*command*" --type=f | head -20Repository: nventive/Promptitude
Length of output: 294
🏁 Script executed:
rg "promptitude-prompts" --type=ts --type=jsonRepository: nventive/Promptitude
Length of output: 163
🏁 Script executed:
cat -n package.json | grep -A 20 -B 5 "commands"Repository: nventive/Promptitude
Length of output: 911
🏁 Script executed:
rg -n "promptitude-prompts" package.json -C3Repository: nventive/Promptitude
Length of output: 46
🏁 Script executed:
cat -n package.json | grep -A 50 '"views"'Repository: nventive/Promptitude
Length of output: 1781
🏁 Script executed:
rg "\.focus" --type=ts src/Repository: nventive/Promptitude
Length of output: 163
🏁 Script executed:
git log --oneline -20 -- package.json src/Repository: nventive/Promptitude
Length of output: 138
🏁 Script executed:
git show ac3c8a4 --statRepository: nventive/Promptitude
Length of output: 2421
🏁 Script executed:
rg "promptitude\.cards\.focus|promptitude\.details\.focus" --type=tsRepository: nventive/Promptitude
Length of output: 46
The command ID promptitude-prompts.focus does not exist and will fail at runtime.
The package.json defines views with IDs promptitude.cards (Prompts) and promptitude.details (Prompt Details). The code attempts to call a non-existent command; it should likely be promptitude.cards.focus to focus the Prompts view, or use an alternative approach if that's not the intended behavior.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/utils/notifications.ts` around lines 165 - 187, In showPromptSizeWarning,
the invoked command ID 'promptitude-prompts.focus' doesn't exist and will throw
at runtime; replace it with the correct view-focus command matching the view ID
in package.json (likely 'promptitude.cards.focus') when calling
vscode.commands.executeCommand('promptitude-prompts.focus'), or if the intent
was to focus the Prompt Details view use 'promptitude.details.focus' instead;
update the executeCommand invocation(s) in showPromptSizeWarning to use the
exact view ID(s) (promptitude.cards.focus or promptitude.details.focus) so the
command resolves successfully.
Problem
When Automatic Sync is enabled with multiple repositories, the extension syncs all prompts and agents (including disabled ones) into the local prompts directory. GitHub Copilot loads all of these as instruction context, which can cause the context/token budget to exceed 100% — effectively blocking users from interacting with Copilot.
Solution
Added a post-sync validation step that detects when the active prompts are too large and warns the user with actionable next steps.
What changed:
src/constant.tssrc/utils/notifications.tsshowPromptSizeWarning()— always-visible warning with "Manage Prompts" and "Open Settings" actionssrc/syncManager.tscleanupInactivePrompts()into sync lifecycle to exclude disabled prompts; addedvalidatePromptSize()that scans the prompts directory after sync and triggers the warning if thresholds are exceededCHANGELOG.md[Unreleased]How it works
showNotificationssetting — it's a safeguard, not informational.No breaking changes
Screenshot
Summary by CodeRabbit