Commit a997992
committed
perf(session): lazy boundary scan + context-window message windowing
Two optimizations to drastically reduce memory during prompting:
1. filterCompactedLazy: probe newest 50 message infos (1 query, no
parts) to detect compaction. If none found, fall back to original
single-pass filterCompacted(stream()) — avoids 155+ wasted info-only
queries for uncompacted sessions. Compacted sessions still use the
efficient two-pass scan.
2. Context-window windowing: before calling toModelMessages, estimate
which messages from the tail fit in the LLM context window using
model.limit.context * 4 chars/token. Only convert those messages to
ModelMessage format. For a 7,704-message session where ~200 fit in
context, this reduces toModelMessages input from 7,704 to ~200
messages — cutting ~300MB of wrapper objects across 4-5 copy layers
down to ~10MB.
Also caches conversation across prompt loop iterations — full reload
only after compaction, incremental merge for tool-call steps.1 parent d500a84 commit a997992
2 files changed
+146
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
913 | 913 | | |
914 | 914 | | |
915 | 915 | | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
916 | 1018 | | |
917 | 1019 | | |
918 | 1020 | | |
919 | 1021 | | |
| 1022 | + | |
920 | 1023 | | |
921 | 1024 | | |
922 | 1025 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
298 | 304 | | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
299 | 309 | | |
300 | 310 | | |
301 | 311 | | |
302 | | - | |
303 | 312 | | |
304 | 313 | | |
305 | 314 | | |
| |||
536 | 545 | | |
537 | 546 | | |
538 | 547 | | |
| 548 | + | |
539 | 549 | | |
540 | 550 | | |
541 | 551 | | |
| |||
550 | 560 | | |
551 | 561 | | |
552 | 562 | | |
| 563 | + | |
553 | 564 | | |
554 | 565 | | |
555 | 566 | | |
| |||
565 | 576 | | |
566 | 577 | | |
567 | 578 | | |
| 579 | + | |
568 | 580 | | |
569 | 581 | | |
570 | 582 | | |
| |||
684 | 696 | | |
685 | 697 | | |
686 | 698 | | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
687 | 717 | | |
688 | 718 | | |
689 | 719 | | |
| |||
692 | 722 | | |
693 | 723 | | |
694 | 724 | | |
695 | | - | |
| 725 | + | |
696 | 726 | | |
697 | 727 | | |
698 | 728 | | |
| |||
740 | 770 | | |
741 | 771 | | |
742 | 772 | | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
743 | 784 | | |
744 | 785 | | |
745 | 786 | | |
| |||
0 commit comments