maf-compaction 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maf_compaction/__init__.py +177 -0
- maf_compaction/_anchored.py +865 -0
- maf_compaction/_composed.py +1394 -0
- maf_compaction/_nested.py +38 -0
- maf_compaction/_preserve.py +176 -0
- maf_compaction/_summary_links.py +26 -0
- maf_compaction/_toolsummary.py +2352 -0
- maf_compaction/_usersummary.py +1681 -0
- maf_compaction/py.typed +0 -0
- maf_compaction-0.1.0.dist-info/METADATA +166 -0
- maf_compaction-0.1.0.dist-info/RECORD +13 -0
- maf_compaction-0.1.0.dist-info/WHEEL +4 -0
- maf_compaction-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Compaction strategies that keep the prompt cache.
|
|
2
|
+
|
|
3
|
+
Provider prompt caches match on exact prefixes, and compaction rewrites history by
|
|
4
|
+
construction. Each strategy here is shaped to break the cached prefix as rarely and as late
|
|
5
|
+
as it can:
|
|
6
|
+
|
|
7
|
+
- :class:`AnchoredCompactionStrategy` keeps a fixed head and tail verbatim and collapses the
|
|
8
|
+
band between them by a rule that reads a group's *position* and nothing else, so the same
|
|
9
|
+
prefix compacts to the same bytes on every later turn and stays cached.
|
|
10
|
+
- :class:`MinimumGainAnchoredCompactionStrategy` adds a floor: it projects the reduction
|
|
11
|
+
before mutating anything and declines a collapse too small to repay the prompt cache it
|
|
12
|
+
would invalidate.
|
|
13
|
+
- :class:`ToolResultAnchoredSummarizationCompactionStrategy` has the agent write the facts
|
|
14
|
+
from its tool results into a record, then drops the tool groups that record covers.
|
|
15
|
+
:func:`make_recall_tool` is the tool the model writes the record with, :class:`RecallGate`
|
|
16
|
+
keeps that tool inert until it is asked for, and :class:`ToolResultRecallMiddleware` asks.
|
|
17
|
+
- :class:`UserTurnAnchoredSummarizationCompactionStrategy` summarises the *user's* turns
|
|
18
|
+
between a fixed head and tail, in one of three modes: recompact its earlier summary once the
|
|
19
|
+
band is worth a pass again, leave it standing as a boundary the next pass compacts only
|
|
20
|
+
behind, or do that and fold the standing summaries into one.
|
|
21
|
+
- :class:`ToolResultAndUserTurnAnchoredSummarizationCompactionStrategy` runs those two over
|
|
22
|
+
one conversation, the record phase first, with the user half judged against the prompt the
|
|
23
|
+
record phase left. While the prompt is still over the input budget it runs a last-resort
|
|
24
|
+
chain: merge the records, merge the user summaries, rewrite the record harder, then the
|
|
25
|
+
record phase's fallback. Every replacement is kept only if it is smaller than what it
|
|
26
|
+
replaces, and nothing is checked against content.
|
|
27
|
+
- :func:`set_preserved` and :func:`is_preserved` mark a message no strategy may shorten, drop
|
|
28
|
+
or shed, which is how a record survives the fallback that trims tool results.
|
|
29
|
+
- :func:`find_nested_strategy` and :func:`record_text` are for the code that wires a strategy
|
|
30
|
+
up: finding the record half inside a composition, and reading the record it wrote.
|
|
31
|
+
|
|
32
|
+
The docstrings keep the vocabulary of the benchmark the strategies were measured with: a *row*
|
|
33
|
+
is one strategy run over one conversation, a row reads *DQ* when its prompt went out over the
|
|
34
|
+
window, and the *flags* are the counters each strategy reports about its own run.
|
|
35
|
+
|
|
36
|
+
**This depends on ``agent_framework._compaction``, which is private API.** Grouping, token
|
|
37
|
+
annotation and the exclusion flags all come from there; nothing public exposes them. The
|
|
38
|
+
dependency is pinned to one minor of the framework for that reason, and each new minor is
|
|
39
|
+
read against before the pin moves.
|
|
40
|
+
|
|
41
|
+
What each strategy does, why it is shaped that way and where it fails is in the compaction
|
|
42
|
+
documentation of the maf-extensions repository.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
from ._anchored import (
|
|
46
|
+
DEFAULT_BAND_SHARE,
|
|
47
|
+
DEFAULT_KEEP_TOKENS,
|
|
48
|
+
DEFAULT_MIN_GAIN_FRACTION,
|
|
49
|
+
MARKER_ID_PREFIX,
|
|
50
|
+
REMOVAL_MARKER,
|
|
51
|
+
AnchoredCompactionStrategy,
|
|
52
|
+
MinimumGainAnchoredCompactionStrategy,
|
|
53
|
+
)
|
|
54
|
+
from ._composed import (
|
|
55
|
+
DEFAULT_CHAIN_GAIN_FRACTION,
|
|
56
|
+
DEFAULT_HARDER_ATTEMPTS,
|
|
57
|
+
ChainDecisions,
|
|
58
|
+
ToolResultAndUserTurnAnchoredSummarizationCompactionStrategy,
|
|
59
|
+
)
|
|
60
|
+
from ._nested import find_nested_strategy
|
|
61
|
+
from ._preserve import (
|
|
62
|
+
PRESERVE_REASON_KEY,
|
|
63
|
+
PRESERVED_KEY,
|
|
64
|
+
any_preserved,
|
|
65
|
+
is_preserved,
|
|
66
|
+
set_preserved,
|
|
67
|
+
)
|
|
68
|
+
from ._toolsummary import (
|
|
69
|
+
DEFAULT_COVERAGE_SHARE,
|
|
70
|
+
DEFAULT_FALLBACK_FRACTION,
|
|
71
|
+
DEFAULT_RECORD_MAX_TOKENS,
|
|
72
|
+
DEFAULT_RECORD_TARGET_TOKENS,
|
|
73
|
+
DEFAULT_TRIGGER_FRACTION,
|
|
74
|
+
PRESERVE_REASON_UNCOVERED,
|
|
75
|
+
PRESERVE_REASON_UNRECORDED,
|
|
76
|
+
RECALL_TOOL_NAME,
|
|
77
|
+
RECORD_MARKER,
|
|
78
|
+
RecallGate,
|
|
79
|
+
RecordDecisions,
|
|
80
|
+
ToolResultAnchoredSummarizationCompactionStrategy,
|
|
81
|
+
ToolResultRecallMiddleware,
|
|
82
|
+
find_record_index,
|
|
83
|
+
make_recall_tool,
|
|
84
|
+
record_text,
|
|
85
|
+
)
|
|
86
|
+
from ._usersummary import (
|
|
87
|
+
DEFAULT_KEEP_HEAD_USER_TURNS,
|
|
88
|
+
DEFAULT_KEEP_TAIL_USER_TURNS,
|
|
89
|
+
DEFAULT_MIN_BAND_SHARE,
|
|
90
|
+
DEFAULT_SUMMARY_MODE,
|
|
91
|
+
DEFAULT_USER_FOLD_PROMPT,
|
|
92
|
+
DEFAULT_USER_SUMMARY_PROMPT,
|
|
93
|
+
DEFAULT_USER_TRIGGER_FRACTION,
|
|
94
|
+
FOLD_EXCLUDE_REASON,
|
|
95
|
+
FOLD_ID_PREFIX,
|
|
96
|
+
SUMMARY_MODE_BOUNDARY,
|
|
97
|
+
SUMMARY_MODE_FOLD,
|
|
98
|
+
SUMMARY_MODE_RECOMPACT,
|
|
99
|
+
SUMMARY_MODES,
|
|
100
|
+
USER_SUMMARY_MARKER,
|
|
101
|
+
UserTurnAnchoredSummarizationCompactionStrategy,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
__all__ = [
|
|
105
|
+
"DEFAULT_BAND_SHARE",
|
|
106
|
+
"DEFAULT_CHAIN_GAIN_FRACTION",
|
|
107
|
+
"DEFAULT_COVERAGE_SHARE",
|
|
108
|
+
"DEFAULT_FALLBACK_FRACTION",
|
|
109
|
+
"DEFAULT_HARDER_ATTEMPTS",
|
|
110
|
+
"DEFAULT_KEEP_HEAD_USER_TURNS",
|
|
111
|
+
"DEFAULT_KEEP_TAIL_USER_TURNS",
|
|
112
|
+
"DEFAULT_KEEP_TOKENS",
|
|
113
|
+
"DEFAULT_MIN_BAND_SHARE",
|
|
114
|
+
"DEFAULT_MIN_GAIN_FRACTION",
|
|
115
|
+
"DEFAULT_RECORD_MAX_TOKENS",
|
|
116
|
+
"DEFAULT_RECORD_TARGET_TOKENS",
|
|
117
|
+
"DEFAULT_SUMMARY_MODE",
|
|
118
|
+
"DEFAULT_TRIGGER_FRACTION",
|
|
119
|
+
"DEFAULT_USER_FOLD_PROMPT",
|
|
120
|
+
"DEFAULT_USER_SUMMARY_PROMPT",
|
|
121
|
+
"DEFAULT_USER_TRIGGER_FRACTION",
|
|
122
|
+
"FOLD_EXCLUDE_REASON",
|
|
123
|
+
"FOLD_ID_PREFIX",
|
|
124
|
+
"MARKER_ID_PREFIX",
|
|
125
|
+
"PRESERVED_KEY",
|
|
126
|
+
"PRESERVE_REASON_KEY",
|
|
127
|
+
"PRESERVE_REASON_UNCOVERED",
|
|
128
|
+
"PRESERVE_REASON_UNRECORDED",
|
|
129
|
+
"RECALL_TOOL_NAME",
|
|
130
|
+
"RECORD_MARKER",
|
|
131
|
+
"REMOVAL_MARKER",
|
|
132
|
+
"SUMMARY_MODES",
|
|
133
|
+
"SUMMARY_MODE_BOUNDARY",
|
|
134
|
+
"SUMMARY_MODE_FOLD",
|
|
135
|
+
"SUMMARY_MODE_RECOMPACT",
|
|
136
|
+
"USER_SUMMARY_MARKER",
|
|
137
|
+
"AnchoredCompactionStrategy",
|
|
138
|
+
"ChainDecisions",
|
|
139
|
+
"MafCompactionExperimentalWarning",
|
|
140
|
+
"MinimumGainAnchoredCompactionStrategy",
|
|
141
|
+
"RecallGate",
|
|
142
|
+
"RecordDecisions",
|
|
143
|
+
"ToolResultAnchoredSummarizationCompactionStrategy",
|
|
144
|
+
"ToolResultAndUserTurnAnchoredSummarizationCompactionStrategy",
|
|
145
|
+
"ToolResultRecallMiddleware",
|
|
146
|
+
"UserTurnAnchoredSummarizationCompactionStrategy",
|
|
147
|
+
"any_preserved",
|
|
148
|
+
"find_nested_strategy",
|
|
149
|
+
"find_record_index",
|
|
150
|
+
"is_preserved",
|
|
151
|
+
"make_recall_tool",
|
|
152
|
+
"record_text",
|
|
153
|
+
"set_preserved",
|
|
154
|
+
]
|
|
155
|
+
|
|
156
|
+
# Experimental package: importing it emits a warning, and catches it when `python -W error`
|
|
157
|
+
# promotes it, so such a host can still import the package.
|
|
158
|
+
import warnings as _warnings
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class MafCompactionExperimentalWarning(UserWarning):
|
|
162
|
+
"""Warning category for maf-compaction's experimental-package notice."""
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _warn_experimental() -> None:
|
|
166
|
+
message = (
|
|
167
|
+
"maf_compaction is experimental and may change or be removed in future versions "
|
|
168
|
+
"without notice."
|
|
169
|
+
)
|
|
170
|
+
try:
|
|
171
|
+
_warnings.warn(message, category=MafCompactionExperimentalWarning, stacklevel=2)
|
|
172
|
+
except MafCompactionExperimentalWarning:
|
|
173
|
+
# Deliberate: under `-W error` an informational notice must not fail the import.
|
|
174
|
+
pass
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
_warn_experimental()
|