Skip to content

fix: add prompt size safeguard to prevent Copilot context overflow#34

Open
mohamedsedkiyouzbechi wants to merge 1 commit intomainfrom
msyo/fix/prompt-size-safeguard
Open

fix: add prompt size safeguard to prevent Copilot context overflow#34
mohamedsedkiyouzbechi wants to merge 1 commit intomainfrom
msyo/fix/prompt-size-safeguard

Conversation

@mohamedsedkiyouzbechi
Copy link
Contributor

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi commented Mar 13, 2026

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:

File Change
src/constant.ts Added configurable threshold constants: 500 KB total size, 50 active prompts, 10 repositories
src/utils/notifications.ts Added showPromptSizeWarning() — always-visible warning with "Manage Prompts" and "Open Settings" actions
src/syncManager.ts Wired cleanupInactivePrompts() into sync lifecycle to exclude disabled prompts; added validatePromptSize() that scans the prompts directory after sync and triggers the warning if thresholds are exceeded
CHANGELOG.md Documented the new safeguard under [Unreleased]

How it works

  1. After each sync completes, inactive prompts are removed from the prompts directory (so Copilot never loads them).
  2. The remaining active prompt files are measured (total bytes + count).
  3. If any threshold is exceeded (total size > 500 KB, active count > 50, or repos > 10), a warning notification is shown with specific reasons and two action buttons.
  4. The warning is always shown regardless of the showNotifications setting — it's a safeguard, not informational.

No breaking changes

  • Thresholds are constants that can be tuned without config changes.
  • The warning is non-blocking — sync completes normally regardless.
  • Disabled prompts were already tracked; now they're actually removed from the active directory during sync.

Screenshot

Screenshot 2026-03-13 133133

Summary by CodeRabbit

  • New Features
    • Prompt size safeguard that warns when active prompts may exceed GitHub Copilot's context window based on file size, prompt count, or repository count
    • Automatic cleanup of disabled prompts during sync
    • Warning notifications with "Manage Prompts" and "Open Settings" action buttons

Copilot AI review requested due to automatic review settings March 13, 2026 14:12
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 13, 2026

Walkthrough

This 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

Cohort / File(s) Summary
Configuration & Constants
src/constant.ts
Added three new warning threshold constants: PROMPT_SIZE_WARNING_THRESHOLD_BYTES (500 KB), PROMPT_COUNT_WARNING_THRESHOLD (50), and REPO_COUNT_WARNING_THRESHOLD (10).
Core Validation Logic
src/syncManager.ts
Implemented private validatePromptSize() method that calculates total prompt size, active prompt count, and repository count with cross-platform file system handling. Integrated validation into sync flow after cleanup and symlink recreation, triggering notifications when thresholds are exceeded.
User Notifications
src/utils/notifications.ts
Added public showPromptSizeWarning() method to display warnings with details (size in KB, counts, reasons) and actionable buttons ("Manage Prompts", "Open Settings", "Dismiss").
Documentation
CHANGELOG.md
Documented two new features: prompt size safeguard with evaluation criteria and cleanup of inactive prompts from active directory during sync.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • Promptitude UI #19: Implements UI components and logic that integrate with the validatePromptSize and notification methods introduced in this PR, particularly around prompt management and settings navigation.
  • Fix Windows compatibility and critical UI bugs in prompts view #23: Modifies SyncManager's prompt activation and cleanup flows where this PR adds the validatePromptSize check, with overlapping changes to prompt-related constants and symlink handling behavior.

Suggested reviewers

  • MatFillion
  • jeanplevesque
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and clearly summarizes the primary change: adding a safeguard to prevent Copilot context overflow by validating prompt sizes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch msyo/fix/prompt-size-safeguard
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Migrating from UI to YAML configuration.

Use the @coderabbitai configuration command in a PR comment to get a dump of all your UI settings in YAML format. You can then edit this YAML file and upload it to the root of your repository to configure CodeRabbit programmatically.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');
Comment on lines +105 to +107
// Remove disabled prompts from the active prompts directory
await this.cleanupInactivePrompts();

Comment on lines +1241 to +1258
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;
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between b2f75d0 and ac3c8a4.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • src/constant.ts
  • src/syncManager.ts
  • src/utils/notifications.ts

Comment on lines +165 to +187

/**
* 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');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for the command registration to ensure it exists
rg -n "promptitude-prompts" --type=ts -C2

Repository: nventive/Promptitude

Length of output: 476


🏁 Script executed:

rg -n "registerCommand.*promptitude-prompts" --type=ts

Repository: nventive/Promptitude

Length of output: 46


🏁 Script executed:

fd -name "extension.ts" -o -name "activate.ts" -o -name "*command*" --type=f | head -20

Repository: nventive/Promptitude

Length of output: 294


🏁 Script executed:

rg "promptitude-prompts" --type=ts --type=json

Repository: 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 -C3

Repository: 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 --stat

Repository: nventive/Promptitude

Length of output: 2421


🏁 Script executed:

rg "promptitude\.cards\.focus|promptitude\.details\.focus" --type=ts

Repository: 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.

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