Repository navigation
Commit b06207a
fix: count what is actually sent when clamping the output reservation
Review of the clamp found five ways the guard read the wrong numbers. Two were
regressions the clamp itself introduced, so they are fixed here rather than
deferred.
- count tool definitions in the estimate. Providers bill tool schemas as prompt
tokens, so omitting them under-counted exactly the tool-heavy sessions most
likely to overflow. In `session/llm.ts` the clamp moves below `resolveTools`
and the retrieval filter — still after the `chat.params` hook — so it sees the
tools actually sent; `session/llm/request.ts` already had them in scope.
- strip media before estimating. Attachments arrive as base64 payloads or byte
arrays on the model messages, and `JSON.stringify` counted a 1 MiB screenshot
as hundreds of thousands of text tokens, refusing a multimodal request the
provider would have accepted. `Compaction.estimate` already does this via
`stripMedia`; this follows the same trade-off.
- apply the estimator margin to the fit check, not just to the clamp. The
boundary was discontinuous: one token over clamped to a ~1K cushion, one token
under was sent with no protection at all, even though the input count is a
character-ratio estimate.
- clamp models that declare `limit.input`. That field is an input ceiling inside
the shared window, not a separate budget: `Session.Overflow.usable` subtracts
the reserved completion from it, and catalog-shaped fixtures pair it with an
equal or larger `context` (context 200K / input 200K / output 32K). The early
return left the exact 400 this guard prevents.
- clamp the configured reasoning budget alongside `maxOutputTokens`. Anthropic
rejects a request whose thinking budget is not below `max_tokens`, and this
repository configures fixed 16,000/31,999-token budgets, so a clamp on its own
turned one provider 400 into another. The floor also rises when a reasoning
variant is configured so thinking and an answer both fit.
Also honours a context window widened by a request header — the GitLab
AI-gateway loader always sends `anthropic-beta: context-1m-2025-08-07` while its
catalog entries still declare 200K — and takes the prompt estimate lazily so the
history is not serialized on paths that return before it is needed.
13 new tests; the two that pinned the old exact-fill and `limit.input` behaviour
are rewritten to assert the corrected outcome.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ1 parent 8c289cb commit b06207a
4 files changed
Lines changed: 314 additions & 47 deletions
File tree
- packages/opencode
- src
- provider
- session
- llm
- test/provider
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1334 | 1334 | | |
1335 | 1335 | | |
1336 | 1336 | | |
1337 | | - | |
1338 | | - | |
1339 | | - | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
1340 | 1425 | | |
1341 | 1426 | | |
1342 | 1427 | | |
1343 | | - | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
1344 | 1435 | | |
1345 | | - | |
1346 | | - | |
1347 | | - | |
1348 | | - | |
1349 | | - | |
1350 | | - | |
| 1436 | + | |
| 1437 | + | |
1351 | 1438 | | |
1352 | 1439 | | |
1353 | 1440 | | |
1354 | 1441 | | |
1355 | | - | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
1356 | 1447 | | |
1357 | 1448 | | |
1358 | 1449 | | |
1359 | 1450 | | |
1360 | | - | |
1361 | | - | |
1362 | | - | |
1363 | | - | |
1364 | | - | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
1365 | 1469 | | |
1366 | | - | |
1367 | | - | |
1368 | | - | |
| 1470 | + | |
| 1471 | + | |
1369 | 1472 | | |
1370 | 1473 | | |
1371 | 1474 | | |
1372 | | - | |
| 1475 | + | |
1373 | 1476 | | |
1374 | 1477 | | |
1375 | | - | |
| 1478 | + | |
1376 | 1479 | | |
1377 | 1480 | | |
1378 | 1481 | | |
1379 | 1482 | | |
1380 | 1483 | | |
1381 | 1484 | | |
1382 | | - | |
| 1485 | + | |
1383 | 1486 | | |
1384 | 1487 | | |
1385 | 1488 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | 153 | | |
166 | 154 | | |
167 | 155 | | |
| |||
223 | 211 | | |
224 | 212 | | |
225 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
226 | 235 | | |
227 | 236 | | |
228 | 237 | | |
| |||
257 | 266 | | |
258 | 267 | | |
259 | 268 | | |
260 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
261 | 272 | | |
262 | 273 | | |
263 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
177 | 185 | | |
178 | 186 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
184 | 190 | | |
185 | 191 | | |
186 | 192 | | |
| |||
0 commit comments