Commit 3e03a30
Python: Implement annotation-based context compaction (#4469)
* Implement annotation-based context compaction
Co-authored-by: Copilot <[email protected]>
* Handle missing compaction attributes in BaseChatClient
Co-authored-by: Copilot <[email protected]>
* Fix CI typing and bandit issues
Co-authored-by: Copilot <[email protected]>
* Optimize incremental compaction annotation pass
Co-authored-by: Copilot <[email protected]>
* refinement
* Python: add ToolResultCompactionStrategy and CompactionProvider
Add ToolResultCompactionStrategy that collapses older tool-call groups
into short summary messages (e.g. [Tool calls: get_weather]) while
keeping the most recent groups verbatim. This mirrors the .NET
ToolResultCompactionStrategy from PR #4533.
Add CompactionProvider as a context-provider that auto-applies compaction
before each agent turn and stores compacted history in session state
after each turn.
Includes tests and samples for both features.
Co-authored-by: Copilot <[email protected]>
* refinement and alignment with dotnet PR
* updated tool result compaction
* updated tool result compaction
* Python: add ToolResultCompactionStrategy, CompactionProvider, and skip_excluded
- ToolResultCompactionStrategy collapses older tool-call groups into
[Tool results: func_name: result] summaries with bidirectional tracing
(same pattern as SummarizationStrategy).
- CompactionProvider as BaseContextProvider with separate before_strategy
and after_strategy parameters. before_strategy compacts loaded context;
after_strategy compacts stored history via history_source_id.
- InMemoryHistoryProvider gains skip_excluded flag to filter out messages
marked as excluded by compaction strategies.
- Tests, samples, and exports updated.
Co-authored-by: Copilot <[email protected]>
* fixed checks
* fix mypy
* Fix: ensure summary messages from both strategies get full compaction annotations
SummarizationStrategy was not calling annotate_message_groups after
inserting its summary message, so the summary lacked core group
annotations (id, kind, index, has_reasoning, _excluded). Added the
missing call. ToolResultCompactionStrategy already had it.
Added tests verifying both strategies produce fully annotated summaries.
Co-authored-by: Copilot <[email protected]>
* updated propagation
* fix mypy
---------
Co-authored-by: Copilot <[email protected]>1 parent 565c0b1 commit 3e03a30
29 files changed
Lines changed: 4401 additions & 209 deletions
File tree
- docs/decisions
- python
- packages
- azure-ai-search/tests
- core
- agent_framework
- openai
- tests
- core
- openai
- samples/02-agents/compaction
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1240 | 1240 | | |
1241 | 1241 | | |
1242 | 1242 | | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
19 | 26 | | |
20 | 27 | | |
21 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
32 | 60 | | |
33 | 61 | | |
34 | 62 | | |
| |||
196 | 224 | | |
197 | 225 | | |
198 | 226 | | |
| 227 | + | |
199 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
200 | 240 | | |
201 | 241 | | |
202 | 242 | | |
| |||
218 | 258 | | |
219 | 259 | | |
220 | 260 | | |
| 261 | + | |
221 | 262 | | |
222 | 263 | | |
223 | 264 | | |
| |||
227 | 268 | | |
228 | 269 | | |
229 | 270 | | |
| 271 | + | |
| 272 | + | |
230 | 273 | | |
231 | 274 | | |
232 | 275 | | |
| |||
273 | 316 | | |
274 | 317 | | |
275 | 318 | | |
| 319 | + | |
276 | 320 | | |
277 | 321 | | |
278 | 322 | | |
279 | 323 | | |
280 | 324 | | |
281 | 325 | | |
282 | 326 | | |
| 327 | + | |
283 | 328 | | |
284 | 329 | | |
| 330 | + | |
285 | 331 | | |
286 | 332 | | |
287 | 333 | | |
| |||
294 | 340 | | |
295 | 341 | | |
296 | 342 | | |
| 343 | + | |
| 344 | + | |
297 | 345 | | |
| 346 | + | |
298 | 347 | | |
| 348 | + | |
299 | 349 | | |
300 | 350 | | |
301 | 351 | | |
| |||
322 | 372 | | |
323 | 373 | | |
324 | 374 | | |
| 375 | + | |
| 376 | + | |
325 | 377 | | |
326 | 378 | | |
327 | 379 | | |
328 | 380 | | |
329 | 381 | | |
330 | 382 | | |
| 383 | + | |
| 384 | + | |
331 | 385 | | |
332 | 386 | | |
333 | 387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| |||
177 | 178 | | |
178 | 179 | | |
179 | 180 | | |
| 181 | + | |
| 182 | + | |
180 | 183 | | |
181 | 184 | | |
182 | 185 | | |
| |||
665 | 668 | | |
666 | 669 | | |
667 | 670 | | |
| 671 | + | |
| 672 | + | |
668 | 673 | | |
669 | 674 | | |
670 | 675 | | |
| |||
688 | 693 | | |
689 | 694 | | |
690 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
691 | 700 | | |
692 | 701 | | |
693 | 702 | | |
| |||
705 | 714 | | |
706 | 715 | | |
707 | 716 | | |
| 717 | + | |
| 718 | + | |
708 | 719 | | |
709 | 720 | | |
710 | 721 | | |
| |||
799 | 810 | | |
800 | 811 | | |
801 | 812 | | |
| 813 | + | |
| 814 | + | |
802 | 815 | | |
803 | 816 | | |
804 | 817 | | |
| |||
811 | 824 | | |
812 | 825 | | |
813 | 826 | | |
| 827 | + | |
| 828 | + | |
814 | 829 | | |
815 | 830 | | |
816 | 831 | | |
| |||
823 | 838 | | |
824 | 839 | | |
825 | 840 | | |
| 841 | + | |
| 842 | + | |
826 | 843 | | |
827 | 844 | | |
828 | 845 | | |
| |||
834 | 851 | | |
835 | 852 | | |
836 | 853 | | |
| 854 | + | |
| 855 | + | |
837 | 856 | | |
838 | 857 | | |
839 | 858 | | |
| |||
857 | 876 | | |
858 | 877 | | |
859 | 878 | | |
860 | | - | |
861 | | - | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
862 | 887 | | |
863 | 888 | | |
864 | 889 | | |
| |||
873 | 898 | | |
874 | 899 | | |
875 | 900 | | |
| 901 | + | |
| 902 | + | |
876 | 903 | | |
877 | 904 | | |
878 | 905 | | |
| |||
881 | 908 | | |
882 | 909 | | |
883 | 910 | | |
| 911 | + | |
| 912 | + | |
884 | 913 | | |
885 | 914 | | |
886 | 915 | | |
| |||
954 | 983 | | |
955 | 984 | | |
956 | 985 | | |
| 986 | + | |
| 987 | + | |
957 | 988 | | |
958 | 989 | | |
959 | 990 | | |
960 | 991 | | |
961 | 992 | | |
962 | 993 | | |
963 | 994 | | |
| 995 | + | |
| 996 | + | |
964 | 997 | | |
965 | 998 | | |
966 | 999 | | |
| |||
1047 | 1080 | | |
1048 | 1081 | | |
1049 | 1082 | | |
| 1083 | + | |
| 1084 | + | |
1050 | 1085 | | |
1051 | 1086 | | |
1052 | 1087 | | |
| |||
1081 | 1116 | | |
1082 | 1117 | | |
1083 | 1118 | | |
| 1119 | + | |
| 1120 | + | |
1084 | 1121 | | |
1085 | 1122 | | |
1086 | | - | |
1087 | 1123 | | |
1088 | 1124 | | |
1089 | 1125 | | |
| |||
1153 | 1189 | | |
1154 | 1190 | | |
1155 | 1191 | | |
| 1192 | + | |
| 1193 | + | |
1156 | 1194 | | |
1157 | 1195 | | |
1158 | 1196 | | |
| |||
1408 | 1446 | | |
1409 | 1447 | | |
1410 | 1448 | | |
| 1449 | + | |
| 1450 | + | |
1411 | 1451 | | |
1412 | 1452 | | |
1413 | 1453 | | |
| |||
1421 | 1461 | | |
1422 | 1462 | | |
1423 | 1463 | | |
| 1464 | + | |
| 1465 | + | |
1424 | 1466 | | |
1425 | 1467 | | |
0 commit comments