Skip to content

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

Merged
mohamedsedkiyouzbechi merged 1 commit intomainfrom
msyo/fix/prompt-size-safeguard
Mar 23, 2026
Merged

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

Conversation

@mohamedsedkiyouzbechi
Copy link
Copy Markdown
Contributor

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi commented Mar 13, 2026

Problem

When Automatic Sync is enabled with multiple repositories, the extension syncs all prompts, agents, and instructions into the local prompts directory.
GitHub Copilot automatically loads .instructions.md files into its context window, 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 active instructions 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 instructions, 10 repositories (INSTRUCTION_SIZE_WARNING_THRESHOLD_BYTES, INSTRUCTION_COUNT_WARNING_THRESHOLD, REPO_COUNT_WARNING_THRESHOLD)
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 only .instructions.md files 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 instruction files (.instructions.md) are measured (total bytes + count). Only instructions are counted because they are auto-loaded into Copilot's context — .prompt.md files are invoked on-demand and don't consume context window.
  3. If any threshold is exceeded (total size > 500 KB, active instructions > 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

image

Summary by CodeRabbit

  • New Features
    • Instruction-size safeguard that warns when active instructions likely exceed context limits (checks total size and active count) after sync.
    • Automatic cleanup of disabled prompts during sync so inactive prompts aren't loaded.
    • User-facing prompt-size warning with actions: "Manage Prompts", "Open Settings", and "Dismiss".
    • Improved prompt matching to avoid ambiguities when prompts share names.

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

coderabbitai bot commented Mar 13, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Adds an instruction-size/count safeguard that scans active .instructions.md files during sync and warns when thresholds are exceeded; removes disabled/inactive prompts from the active prompts directory during sync; improves inactive-prompt matching to include workspaceName; adds file-size API and a dedicated prompt-size notification.

Changes

Cohort / File(s) Summary
Changelog
CHANGELOG.md
Added new 1.5.6 entry describing the instruction-size/count safeguard and inactive-prompt cleanup.
Constants
src/constant.ts
Added INSTRUCTION_SIZE_WARNING_THRESHOLD_BYTES and INSTRUCTION_COUNT_WARNING_THRESHOLD.
Sync logic
src/syncManager.ts
Added validateInstructionSize() and invoke it (after cleanupInactivePrompts()); updated inactive-prompt cleanup matching to consider both prompt.name and prompt.workspaceName; imported new thresholds.
Notifications
src/utils/notifications.ts
Added NotificationManager.showPromptSizeWarning(details) which shows a warning with actions: Manage Prompts, Open Settings, Dismiss.
Filesystem utility
src/utils/fileSystem.ts
Added FileSystemManager.getFileSize(filePath: string): Promise<number>.
Manifest
package.json
Bumped extension version to 1.5.6.

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: Recreate active prompt symlinks
    SyncManager->>SyncManager: cleanupInactivePrompts()
    SyncManager->>FileSystem: List `.instructions.md` files and request sizes
    FileSystem-->>SyncManager: Return file list and sizes
    SyncManager->>SyncManager: Compute totalSizeKB and activeCount
    alt Thresholds exceeded
        SyncManager->>NotificationManager: showPromptSizeWarning(details)
        NotificationManager->>UI: Display warning with actions
        UI-->>User: Present "Manage Prompts" / "Open Settings" / "Dismiss"
    else Within limits
        SyncManager->>SyncManager: Continue and complete sync
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • jeanplevesque
  • Soap-141
🚥 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 clearly and specifically summarizes the main change: adding a size safeguard mechanism to prevent Copilot context overflow, which is the primary objective across all modified files.
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
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch msyo/fix/prompt-size-safeguard

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.

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi force-pushed the msyo/fix/prompt-size-safeguard branch from ea4d0be to c558ebe Compare March 16, 2026 10:08
@mohamedsedkiyouzbechi mohamedsedkiyouzbechi force-pushed the msyo/fix/prompt-size-safeguard branch from f8a8b15 to 7f43750 Compare March 17, 2026 11:16
@mohamedsedkiyouzbechi mohamedsedkiyouzbechi changed the title fix: add prompt size safeguard to prevent Copilot context overflow fix: add instruction size safeguard to prevent Copilot context overflow Mar 17, 2026
@Soap-141 Soap-141 requested a review from Copilot March 17, 2026 15:58
Copy link
Copy Markdown
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 to prevent GitHub Copilot context overflow by measuring active .instructions.md files and warning users when size/count/repo thresholds are exceeded, while also cleaning inactive prompts out of the active prompts directory.

Changes:

  • Add instruction size/count/repo-count warning thresholds as constants.
  • Add a dedicated warning notification with actions to help users manage prompts/settings.
  • Run inactive-prompt cleanup and instruction-size validation after each sync; document the feature in the changelog.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/utils/notifications.ts Adds a new warning notification with actions for managing prompts/settings.
src/utils/fileSystem.ts Adds a helper to read file sizes for instruction-size aggregation.
src/syncManager.ts Hooks inactive prompt cleanup into the sync lifecycle; adds instruction-size validation + warning trigger.
src/constant.ts Introduces size/count/repo-count threshold constants.
CHANGELOG.md Documents the new safeguard and inactive prompt cleanup behavior.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Copy Markdown

@Soap-141 Soap-141 left a comment

Choose a reason for hiding this comment

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

Wait for JP's approval.

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi force-pushed the msyo/fix/prompt-size-safeguard branch from a295e77 to a2d194c Compare March 18, 2026 13:20
@mohamedsedkiyouzbechi mohamedsedkiyouzbechi force-pushed the msyo/fix/prompt-size-safeguard branch 2 times, most recently from 86fec93 to de5a15b Compare March 18, 2026 13:44
Copy link
Copy Markdown
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

🤖 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/syncManager.ts`:
- Line 13: The safeguard currently only checks
INSTRUCTION_SIZE_WARNING_THRESHOLD_BYTES and INSTRUCTION_COUNT_WARNING_THRESHOLD
but omits the repository-count threshold; add the repository-count constant to
the import list (e.g., REPO_SYNC_REPO_COUNT_WARNING_THRESHOLD) alongside
REPO_SYNC_PROMPT_PATH/INSTRUCTION_* and then update the safeguard conditional
(the block referencing INSTRUCTION_SIZE_WARNING_THRESHOLD_BYTES and
INSTRUCTION_COUNT_WARNING_THRESHOLD around the 1305–1338 logic) to also check
repoCount > REPO_SYNC_REPO_COUNT_WARNING_THRESHOLD and include that condition in
the warning message text so the repo-count case triggers the same warning
behavior.
🪄 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: 14d442b0-f950-4410-b516-d2259ec72ca5

📥 Commits

Reviewing files that changed from the base of the PR and between a054e9f and eb48536.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/constant.ts
  • src/syncManager.ts
  • src/utils/fileSystem.ts
  • src/utils/notifications.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/utils/notifications.ts
  • src/utils/fileSystem.ts
  • src/constant.ts

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi requested review from jeanplevesque and removed request for jeanplevesque March 18, 2026 14:46
@mohamedsedkiyouzbechi mohamedsedkiyouzbechi force-pushed the msyo/fix/prompt-size-safeguard branch from 6add00a to d74d6e6 Compare March 23, 2026 15:17
Copy link
Copy Markdown
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.

🧹 Nitpick comments (1)
src/syncManager.ts (1)

1300-1352: Well-structured safeguard with one minor terminology inconsistency to address.

The method correctly targets only .instructions.md files (which are auto-loaded into Copilot's context), has appropriate error handling at both per-file and method levels, and provides clear threshold violation reasons to the user.

The error log at line 1350 should say "Failed to validate instruction size" rather than "Failed to validate prompt size" to align with the method name and JSDoc terminology.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/syncManager.ts` around lines 1300 - 1352, In validateInstructionSize(),
update the final error log message to use the correct terminology: change the
logger.error call that currently logs "Failed to validate prompt size" to
"Failed to validate instruction size" so it matches the method name and JSDoc;
locate the logger.error invocation inside validateInstructionSize (the catch
block that currently passes error or undefined) and replace only the message
string.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/syncManager.ts`:
- Around line 1300-1352: In validateInstructionSize(), update the final error
log message to use the correct terminology: change the logger.error call that
currently logs "Failed to validate prompt size" to "Failed to validate
instruction size" so it matches the method name and JSDoc; locate the
logger.error invocation inside validateInstructionSize (the catch block that
currently passes error or undefined) and replace only the message string.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6a2180bf-c9f4-4150-a85f-322fc93b849f

📥 Commits

Reviewing files that changed from the base of the PR and between eb48536 and d74d6e6.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • package.json
  • src/constant.ts
  • src/syncManager.ts
  • src/utils/fileSystem.ts
  • src/utils/notifications.ts
✅ Files skipped from review due to trivial changes (3)
  • package.json
  • src/constant.ts
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/utils/fileSystem.ts
  • src/utils/notifications.ts

@mohamedsedkiyouzbechi mohamedsedkiyouzbechi merged commit 7af5237 into main Mar 23, 2026
4 checks passed
@mohamedsedkiyouzbechi mohamedsedkiyouzbechi deleted the msyo/fix/prompt-size-safeguard branch March 23, 2026 15:47
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.

4 participants