Commit 54da220
Enforce per-type safe-output max count at MCP invocation time (MCE4) (#40348)
* Initial plan
* Enforce per-type safe-output max count at MCP invocation time (MCE4)
Add session-scoped per-type operation counters inside createHandlers that
enforce the user-configured max at tool-invocation time (the MCP-server half
of MCE4 dual enforcement). Previously the count cap was only applied
downstream in collect_ndjson_output.cjs / safe_output_processor.cjs, so the
agent received no in-loop feedback and silently over-produced outputs that
were then dropped.
Changes:
- operationCounts Map tracks how many items of each type have been appended
during the current MCP session.
- getExplicitMax(type) reads the user's explicit max: setting via
getSafeOutputsToolConfig; returns null when no explicit limit is set or
when max: -1 (unlimited), so only intentional limits are enforced.
- enforcePerTypeMax(type) throws a JSON-RPC -32602 error with E002 code and
actionable guidance when the configured limit has already been reached.
- appendSafeOutputCounted(entry) wraps appendSafeOutput: checks the limit,
delegates to the real append, then increments the counter only on success
(mirrors the inlineReviewCommentCount pattern).
- All appendSafeOutput(entry) call sites within createHandlers replaced with
appendSafeOutputCounted(entry).
Downstream enforcement in collect_ndjson_output.cjs and
safe_output_processor.cjs is preserved unchanged as the processor-time
defence-in-depth layer (MCE4).
10 new tests added covering: limit enforcement, key normalisation,
addCommentHandler, independent per-type budgets, unlimited (-1), unconfigured
types, error message content, failed-write counter isolation, and fresh
counter per createHandlers() call.
Closes #40311
Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com>
* fix: run Prettier on safe_outputs_handlers.test.cjs to fix lint-cjs CI failure
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com>1 parent daa889e commit 54da220
2 files changed
Lines changed: 268 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
206 | 280 | | |
207 | 281 | | |
208 | 282 | | |
| |||
258 | 332 | | |
259 | 333 | | |
260 | 334 | | |
261 | | - | |
| 335 | + | |
262 | 336 | | |
263 | 337 | | |
264 | 338 | | |
| |||
286 | 360 | | |
287 | 361 | | |
288 | 362 | | |
289 | | - | |
| 363 | + | |
290 | 364 | | |
291 | 365 | | |
292 | 366 | | |
| |||
416 | 490 | | |
417 | 491 | | |
418 | 492 | | |
419 | | - | |
| 493 | + | |
420 | 494 | | |
421 | 495 | | |
422 | 496 | | |
| |||
620 | 694 | | |
621 | 695 | | |
622 | 696 | | |
623 | | - | |
| 697 | + | |
624 | 698 | | |
625 | 699 | | |
626 | 700 | | |
| |||
834 | 908 | | |
835 | 909 | | |
836 | 910 | | |
837 | | - | |
| 911 | + | |
838 | 912 | | |
839 | 913 | | |
840 | 914 | | |
| |||
856 | 930 | | |
857 | 931 | | |
858 | 932 | | |
859 | | - | |
| 933 | + | |
860 | 934 | | |
861 | 935 | | |
862 | 936 | | |
| |||
1277 | 1351 | | |
1278 | 1352 | | |
1279 | 1353 | | |
1280 | | - | |
| 1354 | + | |
1281 | 1355 | | |
1282 | 1356 | | |
1283 | 1357 | | |
| |||
1299 | 1373 | | |
1300 | 1374 | | |
1301 | 1375 | | |
1302 | | - | |
| 1376 | + | |
1303 | 1377 | | |
1304 | 1378 | | |
1305 | 1379 | | |
| |||
1551 | 1625 | | |
1552 | 1626 | | |
1553 | 1627 | | |
1554 | | - | |
| 1628 | + | |
1555 | 1629 | | |
1556 | 1630 | | |
1557 | 1631 | | |
| |||
1572 | 1646 | | |
1573 | 1647 | | |
1574 | 1648 | | |
1575 | | - | |
| 1649 | + | |
1576 | 1650 | | |
1577 | 1651 | | |
1578 | 1652 | | |
| |||
1603 | 1677 | | |
1604 | 1678 | | |
1605 | 1679 | | |
1606 | | - | |
| 1680 | + | |
1607 | 1681 | | |
1608 | 1682 | | |
1609 | 1683 | | |
| |||
1714 | 1788 | | |
1715 | 1789 | | |
1716 | 1790 | | |
1717 | | - | |
| 1791 | + | |
1718 | 1792 | | |
1719 | 1793 | | |
1720 | 1794 | | |
| |||
1893 | 1967 | | |
1894 | 1968 | | |
1895 | 1969 | | |
1896 | | - | |
| 1970 | + | |
1897 | 1971 | | |
1898 | 1972 | | |
1899 | 1973 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2736 | 2736 | | |
2737 | 2737 | | |
2738 | 2738 | | |
| 2739 | + | |
| 2740 | + | |
| 2741 | + | |
| 2742 | + | |
| 2743 | + | |
| 2744 | + | |
| 2745 | + | |
| 2746 | + | |
| 2747 | + | |
| 2748 | + | |
| 2749 | + | |
| 2750 | + | |
| 2751 | + | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
| 2755 | + | |
| 2756 | + | |
| 2757 | + | |
| 2758 | + | |
| 2759 | + | |
| 2760 | + | |
| 2761 | + | |
| 2762 | + | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
| 2770 | + | |
| 2771 | + | |
| 2772 | + | |
| 2773 | + | |
| 2774 | + | |
| 2775 | + | |
| 2776 | + | |
| 2777 | + | |
| 2778 | + | |
| 2779 | + | |
| 2780 | + | |
| 2781 | + | |
| 2782 | + | |
| 2783 | + | |
| 2784 | + | |
| 2785 | + | |
| 2786 | + | |
| 2787 | + | |
| 2788 | + | |
| 2789 | + | |
| 2790 | + | |
| 2791 | + | |
| 2792 | + | |
| 2793 | + | |
| 2794 | + | |
| 2795 | + | |
| 2796 | + | |
| 2797 | + | |
| 2798 | + | |
| 2799 | + | |
| 2800 | + | |
| 2801 | + | |
| 2802 | + | |
| 2803 | + | |
| 2804 | + | |
| 2805 | + | |
| 2806 | + | |
| 2807 | + | |
| 2808 | + | |
| 2809 | + | |
| 2810 | + | |
| 2811 | + | |
| 2812 | + | |
| 2813 | + | |
| 2814 | + | |
| 2815 | + | |
| 2816 | + | |
| 2817 | + | |
| 2818 | + | |
| 2819 | + | |
| 2820 | + | |
| 2821 | + | |
| 2822 | + | |
| 2823 | + | |
| 2824 | + | |
| 2825 | + | |
| 2826 | + | |
| 2827 | + | |
| 2828 | + | |
| 2829 | + | |
| 2830 | + | |
| 2831 | + | |
| 2832 | + | |
| 2833 | + | |
| 2834 | + | |
| 2835 | + | |
| 2836 | + | |
| 2837 | + | |
| 2838 | + | |
| 2839 | + | |
| 2840 | + | |
| 2841 | + | |
| 2842 | + | |
| 2843 | + | |
| 2844 | + | |
| 2845 | + | |
| 2846 | + | |
| 2847 | + | |
| 2848 | + | |
| 2849 | + | |
| 2850 | + | |
| 2851 | + | |
| 2852 | + | |
| 2853 | + | |
| 2854 | + | |
| 2855 | + | |
| 2856 | + | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
| 2862 | + | |
| 2863 | + | |
| 2864 | + | |
| 2865 | + | |
| 2866 | + | |
| 2867 | + | |
| 2868 | + | |
| 2869 | + | |
| 2870 | + | |
| 2871 | + | |
| 2872 | + | |
| 2873 | + | |
| 2874 | + | |
| 2875 | + | |
| 2876 | + | |
| 2877 | + | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
| 2883 | + | |
| 2884 | + | |
| 2885 | + | |
| 2886 | + | |
| 2887 | + | |
| 2888 | + | |
| 2889 | + | |
| 2890 | + | |
| 2891 | + | |
| 2892 | + | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
| 2897 | + | |
| 2898 | + | |
| 2899 | + | |
| 2900 | + | |
| 2901 | + | |
| 2902 | + | |
| 2903 | + | |
| 2904 | + | |
| 2905 | + | |
| 2906 | + | |
| 2907 | + | |
| 2908 | + | |
| 2909 | + | |
| 2910 | + | |
| 2911 | + | |
| 2912 | + | |
| 2913 | + | |
| 2914 | + | |
| 2915 | + | |
| 2916 | + | |
| 2917 | + | |
| 2918 | + | |
| 2919 | + | |
2739 | 2920 | | |
2740 | 2921 | | |
2741 | 2922 | | |
| |||
0 commit comments