Commit 3a52f7b
committed
feat: composable EnableCondition with bitmask optimization
Add a composable EnableCondition system for tool filtering that:
1. **User-facing API** (conditions.go):
- EnableCondition interface with Evaluate(ctx) method
- Primitives: FeatureFlag(), ContextBool(), Static(), Always(), Never()
- Combinators: And(), Or(), Not() with short-circuit evaluation
- All bitmask complexity hidden from users
2. **Bitmask compiler** (condition_compiler.go):
- Compiles conditions to O(1) bitmask evaluators at build time
- RequestMask holds pre-computed uint64 bitmask per request
- AND/OR of flags compile to single bitmask operations
- Falls back gracefully for custom ConditionFunc
3. **Pre-sorting optimization** (builder.go):
- Tools, resources, prompts sorted once at build time
- Filtering preserves order, eliminating per-request sorting
- ~45% faster request handling in benchmarks
4. **Integration** (filters.go, registry.go):
- Builder.Build() compiles all EnableConditions
- AvailableTools() builds RequestMask once, evaluates via bitmask
- Backward compatible with legacy Enabled func and feature flags
Usage example:
tool.EnableCondition = Or(
ContextBool("is_cca"), // CCA users bypass flag
FeatureFlag("my_feature"), // Others need flag enabled
)
Benchmarks (1000 requests × 50 tools):
- Before: 23.7ms (with per-request sorting)
- After: 12.9ms (pre-sorted + bitmask)
- Improvement: 46% faster
This makes it easy for remote server to adopt - just set EnableCondition
on tools and the optimization is automatic.1 parent 3c453dd commit 3a52f7b
File tree
10 files changed
+3429
-51
lines changed- pkg/inventory
10 files changed
+3429
-51
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
| 132 | + | |
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
135 | 143 | | |
136 | | - | |
137 | | - | |
138 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
139 | 147 | | |
140 | 148 | | |
141 | 149 | | |
| |||
158 | 166 | | |
159 | 167 | | |
160 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
161 | 173 | | |
162 | 174 | | |
163 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 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 | + | |
164 | 243 | | |
165 | 244 | | |
166 | 245 | | |
| |||
0 commit comments