Repository navigation
Commit d90355f
Python: exclude encrypted reasoning payloads from compaction token counts (#8354)
* Python: exclude encrypted reasoning payloads from compaction token counts
The compaction token counter serialises each message and tokenises the JSON.
Content.protected_data (OpenAI encrypted_content, Anthropic thinking
signature, Gemini thought_signature, OpenAI reasoning_details) is opaque
ciphertext the model replays but never tokenises, so counting it inflated
every token-aware compaction decision. The same blob can also ride in
additional_properties as encrypted_content.
Both are now excluded from serialised content, matching the existing
exclusions for raw_representation and mirrored items.
Fixes #8348
* fix(compaction): count clear-text reasoning details and invalidate stale token caches
Review follow-up for the protected-data token exclusion:
- reasoning_details stored in protected_data are JSON-serialised and
replayed to the provider as clear text (summary, reasoning_text and
nested reasoning.text), so only opaque members (encrypted_content) are
excluded from the estimate; non-JSON payloads (Anthropic signature,
Responses API encrypted blobs) are still dropped entirely.
- group token counts are stamped with a serialization basis version;
counts cached under an older basis (e.g. sessions annotated before a
serialization change) are recomputed instead of reused.
* fix(compaction): resolve pyright/mypy typing errors in token estimation
- annotate the recursive reasoning-payload filter with explicit
dict[Any, Any] / list[Any] casts so iterating the narrowed Any dict
does not leak Unknown types under pyright strict
- cast additional_properties before the encrypted_content filter
- assert non-None token counts in the clear-text regression test so the
ordering comparison does not involve optional integers
---------
Co-authored-by: Evan Mattson <[email protected]>1 parent 999dda7 commit d90355f
2 files changed
Lines changed: 179 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
33 | 39 | | |
34 | 40 | | |
35 | 41 | | |
| |||
496 | 502 | | |
497 | 503 | | |
498 | 504 | | |
499 | | - | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
500 | 512 | | |
501 | 513 | | |
502 | 514 | | |
| |||
508 | 520 | | |
509 | 521 | | |
510 | 522 | | |
| 523 | + | |
511 | 524 | | |
512 | 525 | | |
513 | 526 | | |
| |||
517 | 530 | | |
518 | 531 | | |
519 | 532 | | |
| 533 | + | |
520 | 534 | | |
521 | 535 | | |
522 | 536 | | |
| |||
550 | 564 | | |
551 | 565 | | |
552 | 566 | | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
553 | 571 | | |
554 | 572 | | |
555 | 573 | | |
| |||
558 | 576 | | |
559 | 577 | | |
560 | 578 | | |
| 579 | + | |
561 | 580 | | |
562 | 581 | | |
563 | 582 | | |
| |||
732 | 751 | | |
733 | 752 | | |
734 | 753 | | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
735 | 780 | | |
736 | 781 | | |
737 | 782 | | |
738 | 783 | | |
739 | 784 | | |
740 | 785 | | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
741 | 816 | | |
742 | 817 | | |
743 | 818 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
438 | 535 | | |
| 536 | + | |
| 537 | + | |
439 | 538 | | |
440 | 539 | | |
441 | 540 | | |
| |||
445 | 544 | | |
446 | 545 | | |
447 | 546 | | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
448 | 551 | | |
449 | 552 | | |
450 | 553 | | |
| |||
0 commit comments