xithead 1.5.0 → 1.5.2
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.
- package/bin/xithead.js +486 -226
- package/{npm/index.js → index.js} +2 -2
- package/package.json +46 -46
- package/.actrc +0 -14
- package/.actrc.local.example +0 -15
- package/.changelog.md +0 -2
- package/.claude/agents/academic-anthropologist.md +0 -125
- package/.claude/agents/academic-geographer.md +0 -127
- package/.claude/agents/academic-historian.md +0 -123
- package/.claude/agents/academic-narratologist.md +0 -118
- package/.claude/agents/academic-psychologist.md +0 -118
- package/.claude/agents/academic-statistician.md +0 -144
- package/.claude/agents/accounts-payable-agent.md +0 -205
- package/.claude/agents/agentic-identity-trust.md +0 -394
- package/.claude/agents/agents-orchestrator.md +0 -367
- package/.claude/agents/automation-governance-architect.md +0 -216
- package/.claude/agents/blender-addon-engineer.md +0 -243
- package/.claude/agents/business-strategist.md +0 -488
- package/.claude/agents/change-management-consultant.md +0 -497
- package/.claude/agents/chief-financial-officer.md +0 -388
- package/.claude/agents/corporate-training-designer.md +0 -192
- package/.claude/agents/customer-service.md +0 -398
- package/.claude/agents/customer-success-manager.md +0 -460
- package/.claude/agents/data-consolidation-agent.md +0 -60
- package/.claude/agents/data-privacy-officer.md +0 -412
- package/.claude/agents/design-brand-guardian.md +0 -322
- package/.claude/agents/design-image-prompt-engineer.md +0 -236
- package/.claude/agents/design-inclusive-visuals-specialist.md +0 -73
- package/.claude/agents/design-persona-walkthrough.md +0 -272
- package/.claude/agents/design-ui-designer.md +0 -386
- package/.claude/agents/design-ui-finish-gate-reviewer.md +0 -217
- package/.claude/agents/design-ux-architect.md +0 -482
- package/.claude/agents/design-ux-researcher.md +0 -329
- package/.claude/agents/design-visual-storyteller.md +0 -149
- package/.claude/agents/design-whimsy-injector.md +0 -460
- package/.claude/agents/economy-designer.md +0 -156
- package/.claude/agents/engineering-ai-data-remediation-engineer.md +0 -211
- package/.claude/agents/engineering-ai-engineer.md +0 -146
- package/.claude/agents/engineering-api-platform-engineer.md +0 -181
- package/.claude/agents/engineering-ats-validator-architect.md +0 -383
- package/.claude/agents/engineering-autonomous-optimization-architect.md +0 -121
- package/.claude/agents/engineering-backend-architect.md +0 -255
- package/.claude/agents/engineering-china-network-engineer.md +0 -252
- package/.claude/agents/engineering-cms-developer.md +0 -536
- package/.claude/agents/engineering-code-reviewer.md +0 -76
- package/.claude/agents/engineering-codebase-onboarding-engineer.md +0 -173
- package/.claude/agents/engineering-data-engineer.md +0 -349
- package/.claude/agents/engineering-data-visualization-engineer.md +0 -151
- package/.claude/agents/engineering-database-optimizer.md +0 -188
- package/.claude/agents/engineering-database-reliability-engineer.md +0 -201
- package/.claude/agents/engineering-desktop-app-engineer.md +0 -209
- package/.claude/agents/engineering-developer-tooling-engineer.md +0 -153
- package/.claude/agents/engineering-devops-automator.md +0 -385
- package/.claude/agents/engineering-drupal-performance.md +0 -347
- package/.claude/agents/engineering-drupal-shopping-cart.md +0 -360
- package/.claude/agents/engineering-electrical-engineer.md +0 -575
- package/.claude/agents/engineering-email-intelligence-engineer.md +0 -390
- package/.claude/agents/engineering-embedded-firmware-engineer.md +0 -196
- package/.claude/agents/engineering-feishu-integration-developer.md +0 -606
- package/.claude/agents/engineering-filament-optimization-specialist.md +0 -283
- package/.claude/agents/engineering-finops-engineer.md +0 -164
- package/.claude/agents/engineering-frontend-developer.md +0 -257
- package/.claude/agents/engineering-gaussdb-expert.md +0 -334
- package/.claude/agents/engineering-git-workflow-master.md +0 -98
- package/.claude/agents/engineering-i18n-engineer.md +0 -214
- package/.claude/agents/engineering-identity-access-engineer.md +0 -217
- package/.claude/agents/engineering-incident-response-commander.md +0 -444
- package/.claude/agents/engineering-iot-fleet-engineer.md +0 -148
- package/.claude/agents/engineering-it-service-manager.md +0 -561
- package/.claude/agents/engineering-knowledge-graph-engineer.md +0 -372
- package/.claude/agents/engineering-llm-post-training-engineer.md +0 -166
- package/.claude/agents/engineering-minimal-change-engineer.md +0 -207
- package/.claude/agents/engineering-mobile-app-builder.md +0 -493
- package/.claude/agents/engineering-mobile-release-engineer.md +0 -164
- package/.claude/agents/engineering-multi-agent-systems-architect.md +0 -600
- package/.claude/agents/engineering-network-engineer.md +0 -245
- package/.claude/agents/engineering-orgscript-engineer.md +0 -113
- package/.claude/agents/engineering-payments-billing-engineer.md +0 -228
- package/.claude/agents/engineering-pdf-engine-architect.md +0 -696
- package/.claude/agents/engineering-platform-engineer.md +0 -270
- package/.claude/agents/engineering-privacy-engineer.md +0 -152
- package/.claude/agents/engineering-prompt-engineer.md +0 -209
- package/.claude/agents/engineering-rag-pipeline-engineer.md +0 -450
- package/.claude/agents/engineering-rapid-prototyper.md +0 -466
- package/.claude/agents/engineering-realtime-collaboration-engineer.md +0 -200
- package/.claude/agents/engineering-rust-refactoring-specialist.md +0 -313
- package/.claude/agents/engineering-search-relevance-engineer.md +0 -237
- package/.claude/agents/engineering-section-508-specialist.md +0 -339
- package/.claude/agents/engineering-senior-developer.md +0 -176
- package/.claude/agents/engineering-servicenow-developer-mentor.md +0 -180
- package/.claude/agents/engineering-software-architect.md +0 -112
- package/.claude/agents/engineering-solidity-smart-contract-engineer.md +0 -522
- package/.claude/agents/engineering-sre.md +0 -90
- package/.claude/agents/engineering-technical-writer.md +0 -393
- package/.claude/agents/engineering-universal-document-compiler.md +0 -370
- package/.claude/agents/engineering-uswds-developer.md +0 -340
- package/.claude/agents/engineering-video-streaming-engineer.md +0 -151
- package/.claude/agents/engineering-voice-ai-integration-engineer.md +0 -584
- package/.claude/agents/engineering-webassembly-engineer.md +0 -153
- package/.claude/agents/engineering-wechat-mini-program-developer.md +0 -356
- package/.claude/agents/engineering-wordpress-performance.md +0 -346
- package/.claude/agents/engineering-wordpress-shopping-cart.md +0 -346
- package/.claude/agents/esg-sustainability-officer.md +0 -396
- package/.claude/agents/finance-bookkeeper-controller.md +0 -260
- package/.claude/agents/finance-financial-analyst.md +0 -234
- package/.claude/agents/finance-fpa-analyst.md +0 -263
- package/.claude/agents/finance-investment-researcher.md +0 -272
- package/.claude/agents/finance-tax-strategist.md +0 -239
- package/.claude/agents/game-audio-engineer.md +0 -264
- package/.claude/agents/game-designer.md +0 -167
- package/.claude/agents/gis-3d-scene-developer.md +0 -111
- package/.claude/agents/gis-analyst.md +0 -91
- package/.claude/agents/gis-bim-specialist.md +0 -108
- package/.claude/agents/gis-cartography-designer.md +0 -150
- package/.claude/agents/gis-drone-reality-mapping.md +0 -120
- package/.claude/agents/gis-geoai-ml-engineer.md +0 -105
- package/.claude/agents/gis-geoprocessing-specialist.md +0 -97
- package/.claude/agents/gis-qa-engineer.md +0 -133
- package/.claude/agents/gis-solution-engineer.md +0 -101
- package/.claude/agents/gis-spatial-data-engineer.md +0 -97
- package/.claude/agents/gis-spatial-data-scientist.md +0 -111
- package/.claude/agents/gis-technical-consultant.md +0 -86
- package/.claude/agents/gis-web-gis-developer.md +0 -108
- package/.claude/agents/godot-gameplay-scripter.md +0 -338
- package/.claude/agents/godot-multiplayer-engineer.md +0 -301
- package/.claude/agents/godot-shader-developer.md +0 -267
- package/.claude/agents/government-digital-presales-consultant.md +0 -363
- package/.claude/agents/grant-writer.md +0 -511
- package/.claude/agents/healthcare-aging-parent-care-companion.md +0 -414
- package/.claude/agents/healthcare-clinical-evidence-agent.md +0 -231
- package/.claude/agents/healthcare-customer-service.md +0 -389
- package/.claude/agents/healthcare-innovation-strategist.md +0 -433
- package/.claude/agents/healthcare-marketing-compliance.md +0 -395
- package/.claude/agents/healthcare-sovereign-health-systems-agent.md +0 -312
- package/.claude/agents/hospitality-guest-services.md +0 -603
- package/.claude/agents/hr-onboarding.md +0 -451
- package/.claude/agents/identity-graph-operator.md +0 -303
- package/.claude/agents/language-translator.md +0 -264
- package/.claude/agents/legal-billing-time-tracking.md +0 -569
- package/.claude/agents/legal-client-intake.md +0 -492
- package/.claude/agents/legal-document-review.md +0 -454
- package/.claude/agents/level-designer.md +0 -208
- package/.claude/agents/loan-officer-assistant.md +0 -555
- package/.claude/agents/lsp-index-engineer.md +0 -320
- package/.claude/agents/ma-integration-manager.md +0 -427
- package/.claude/agents/macos-spatial-metal-engineer.md +0 -337
- package/.claude/agents/marketing-aeo-foundations.md +0 -264
- package/.claude/agents/marketing-agentic-search-optimizer.md +0 -279
- package/.claude/agents/marketing-ai-citation-strategist.md +0 -172
- package/.claude/agents/marketing-app-store-optimizer.md +0 -321
- package/.claude/agents/marketing-baidu-seo-specialist.md +0 -226
- package/.claude/agents/marketing-bilibili-content-strategist.md +0 -199
- package/.claude/agents/marketing-book-co-author.md +0 -110
- package/.claude/agents/marketing-carousel-growth-engine.md +0 -204
- package/.claude/agents/marketing-china-ecommerce-operator.md +0 -283
- package/.claude/agents/marketing-china-market-localization-strategist.md +0 -283
- package/.claude/agents/marketing-content-creator.md +0 -54
- package/.claude/agents/marketing-cross-border-ecommerce.md +0 -259
- package/.claude/agents/marketing-developer-community-builder.md +0 -285
- package/.claude/agents/marketing-douyin-strategist.md +0 -149
- package/.claude/agents/marketing-email-strategist.md +0 -249
- package/.claude/agents/marketing-global-podcast-strategist.md +0 -206
- package/.claude/agents/marketing-growth-hacker.md +0 -54
- package/.claude/agents/marketing-instagram-curator.md +0 -113
- package/.claude/agents/marketing-kuaishou-strategist.md +0 -223
- package/.claude/agents/marketing-linkedin-content-creator.md +0 -214
- package/.claude/agents/marketing-livestream-commerce-coach.md +0 -305
- package/.claude/agents/marketing-multi-platform-publisher.md +0 -217
- package/.claude/agents/marketing-podcast-strategist.md +0 -277
- package/.claude/agents/marketing-pr-communications-manager.md +0 -473
- package/.claude/agents/marketing-private-domain-operator.md +0 -308
- package/.claude/agents/marketing-reddit-community-builder.md +0 -123
- package/.claude/agents/marketing-seo-specialist.md +0 -370
- package/.claude/agents/marketing-short-video-editing-coach.md +0 -412
- package/.claude/agents/marketing-social-media-strategist.md +0 -125
- package/.claude/agents/marketing-tiktok-strategist.md +0 -125
- package/.claude/agents/marketing-twitter-engager.md +0 -126
- package/.claude/agents/marketing-video-optimization-specialist.md +0 -119
- package/.claude/agents/marketing-wechat-official-account.md +0 -145
- package/.claude/agents/marketing-weibo-strategist.md +0 -240
- package/.claude/agents/marketing-x-twitter-intelligence-analyst.md +0 -161
- package/.claude/agents/marketing-xiaohongshu-specialist.md +0 -138
- package/.claude/agents/marketing-zhihu-strategist.md +0 -162
- package/.claude/agents/medical-billing-coding-specialist.md +0 -491
- package/.claude/agents/narrative-designer.md +0 -243
- package/.claude/agents/operations-manager.md +0 -399
- package/.claude/agents/organizational-psychologist.md +0 -391
- package/.claude/agents/paid-media-auditor.md +0 -71
- package/.claude/agents/paid-media-creative-strategist.md +0 -71
- package/.claude/agents/paid-media-paid-social-strategist.md +0 -71
- package/.claude/agents/paid-media-ppc-strategist.md +0 -71
- package/.claude/agents/paid-media-programmatic-buyer.md +0 -71
- package/.claude/agents/paid-media-search-query-analyst.md +0 -71
- package/.claude/agents/paid-media-tracking-specialist.md +0 -71
- package/.claude/agents/personal-growth-mentor.md +0 -159
- package/.claude/agents/product-behavioral-nudge-engine.md +0 -83
- package/.claude/agents/product-dx-engineer.md +0 -332
- package/.claude/agents/product-feedback-synthesizer.md +0 -119
- package/.claude/agents/product-manager.md +0 -469
- package/.claude/agents/product-sprint-prioritizer.md +0 -154
- package/.claude/agents/product-trend-researcher.md +0 -159
- package/.claude/agents/project-management-experiment-tracker.md +0 -198
- package/.claude/agents/project-management-jira-workflow-steward.md +0 -233
- package/.claude/agents/project-management-meeting-notes-specialist.md +0 -95
- package/.claude/agents/project-management-project-shepherd.md +0 -194
- package/.claude/agents/project-management-studio-operations.md +0 -200
- package/.claude/agents/project-management-studio-producer.md +0 -203
- package/.claude/agents/project-manager-senior.md +0 -135
- package/.claude/agents/real-estate-buyer-seller.md +0 -596
- package/.claude/agents/recruitment-specialist.md +0 -515
- package/.claude/agents/report-distribution-agent.md +0 -65
- package/.claude/agents/research-synthesist.md +0 -136
- package/.claude/agents/resume-tailor.md +0 -230
- package/.claude/agents/retail-customer-returns.md +0 -566
- package/.claude/agents/roblox-avatar-creator.md +0 -297
- package/.claude/agents/roblox-experience-designer.md +0 -306
- package/.claude/agents/roblox-systems-scripter.md +0 -332
- package/.claude/agents/sales-account-strategist.md +0 -227
- package/.claude/agents/sales-coach.md +0 -271
- package/.claude/agents/sales-data-extraction-agent.md +0 -67
- package/.claude/agents/sales-deal-strategist.md +0 -180
- package/.claude/agents/sales-discovery-coach.md +0 -225
- package/.claude/agents/sales-engineer.md +0 -182
- package/.claude/agents/sales-offer-lead-gen-strategist.md +0 -257
- package/.claude/agents/sales-outbound-strategist.md +0 -201
- package/.claude/agents/sales-outreach.md +0 -425
- package/.claude/agents/sales-pipeline-analyst.md +0 -267
- package/.claude/agents/sales-proposal-strategist.md +0 -217
- package/.claude/agents/security-ai-generated-code-auditor.md +0 -207
- package/.claude/agents/security-appsec-engineer.md +0 -536
- package/.claude/agents/security-architect.md +0 -307
- package/.claude/agents/security-blockchain-security-auditor.md +0 -482
- package/.claude/agents/security-cloud-security-architect.md +0 -576
- package/.claude/agents/security-compliance-auditor.md +0 -158
- package/.claude/agents/security-incident-responder.md +0 -450
- package/.claude/agents/security-penetration-tester.md +0 -399
- package/.claude/agents/security-secrets-credential-engineer.md +0 -189
- package/.claude/agents/security-senior-secops.md +0 -760
- package/.claude/agents/security-threat-detection-engineer.md +0 -553
- package/.claude/agents/security-threat-intelligence-analyst.md +0 -661
- package/.claude/agents/specialized-chief-of-staff.md +0 -279
- package/.claude/agents/specialized-civil-engineer.md +0 -356
- package/.claude/agents/specialized-codebase-archaeologist.md +0 -341
- package/.claude/agents/specialized-cultural-intelligence-strategist.md +0 -88
- package/.claude/agents/specialized-developer-advocate.md +0 -317
- package/.claude/agents/specialized-document-generator.md +0 -55
- package/.claude/agents/specialized-fedramp-rmf-compliance.md +0 -378
- package/.claude/agents/specialized-focus-music-architect.md +0 -175
- package/.claude/agents/specialized-french-consulting-market.md +0 -194
- package/.claude/agents/specialized-korean-business-navigator.md +0 -216
- package/.claude/agents/specialized-master-plan-architect.md +0 -157
- package/.claude/agents/specialized-mcp-builder.md +0 -260
- package/.claude/agents/specialized-model-qa.md +0 -528
- package/.claude/agents/specialized-pricing-analyst.md +0 -243
- package/.claude/agents/specialized-salesforce-architect.md +0 -182
- package/.claude/agents/specialized-strategy-duel-agent.md +0 -130
- package/.claude/agents/specialized-workflow-architect.md +0 -597
- package/.claude/agents/study-abroad-advisor.md +0 -282
- package/.claude/agents/supply-chain-strategist.md +0 -591
- package/.claude/agents/support-analytics-reporter.md +0 -387
- package/.claude/agents/support-executive-summary-generator.md +0 -212
- package/.claude/agents/support-finance-tracker.md +0 -480
- package/.claude/agents/support-infrastructure-maintainer.md +0 -628
- package/.claude/agents/support-legal-compliance-checker.md +0 -591
- package/.claude/agents/support-support-responder.md +0 -607
- package/.claude/agents/technical-artist.md +0 -233
- package/.claude/agents/terminal-integration-specialist.md +0 -70
- package/.claude/agents/testing-accessibility-auditor.md +0 -327
- package/.claude/agents/testing-api-tester.md +0 -334
- package/.claude/agents/testing-evidence-collector.md +0 -210
- package/.claude/agents/testing-performance-benchmarker.md +0 -283
- package/.claude/agents/testing-reality-checker.md +0 -249
- package/.claude/agents/testing-test-automation-engineer.md +0 -196
- package/.claude/agents/testing-test-results-analyzer.md +0 -330
- package/.claude/agents/testing-tool-evaluator.md +0 -416
- package/.claude/agents/testing-workflow-optimizer.md +0 -463
- package/.claude/agents/unity-architect.md +0 -271
- package/.claude/agents/unity-editor-tool-developer.md +0 -310
- package/.claude/agents/unity-multiplayer-engineer.md +0 -323
- package/.claude/agents/unity-shader-graph-artist.md +0 -269
- package/.claude/agents/unreal-multiplayer-architect.md +0 -364
- package/.claude/agents/unreal-systems-engineer.md +0 -322
- package/.claude/agents/unreal-technical-artist.md +0 -256
- package/.claude/agents/unreal-world-builder.md +0 -273
- package/.claude/agents/visionos-spatial-engineer.md +0 -54
- package/.claude/agents/xr-cockpit-interaction-specialist.md +0 -32
- package/.claude/agents/xr-immersive-developer.md +0 -32
- package/.claude/agents/xr-interface-architect.md +0 -32
- package/.claude/agents/zk-steward.md +0 -211
- package/.claude-plugin/marketplace.json +0 -30
- package/.commitlintrc.json +0 -26
- package/.devcontainer/Dockerfile +0 -24
- package/.devcontainer/devcontainer.json +0 -70
- package/.devcontainer/docker-compose.memory.yml +0 -42
- package/.devcontainer/memory-stack/devcontainer.json +0 -84
- package/.devcontainer/post-create.sh +0 -83
- package/.dockerignore +0 -23
- package/.env.act.example +0 -15
- package/.env.example +0 -171
- package/.eslintrc.json +0 -16
- package/.git-blame-ignore-revs +0 -10
- package/.gitattributes +0 -2
- package/.gitguardian.yaml +0 -50
- package/.githooks/pre-commit +0 -6
- package/.pre-commit-config.yaml +0 -30
- package/.release-please-config-xithead.json +0 -22
- package/.release-please-config.json +0 -44
- package/.release-please-manifest-xithead.json +0 -3
- package/.release-please-manifest.json +0 -3
- package/CODE_OF_CONDUCT.md +0 -133
- package/CONTRIBUTING.md +0 -133
- package/Cargo.lock +0 -5557
- package/Cargo.toml +0 -121
- package/Dockerfile +0 -38
- package/Dockerfile.proxy +0 -48
- package/ENTERPRISE.md +0 -5
- package/FEATURES_ADVANCED.md +0 -311
- package/Headroom-2.gif +0 -0
- package/HeadroomDemo-Fast.gif +0 -0
- package/LICENSE +0 -190
- package/MANIFEST.in +0 -4
- package/MODERNIZATION_GUIDE.md +0 -358
- package/Makefile +0 -140
- package/NOTICE +0 -43
- package/NPM_TOKEN_SETUP.md +0 -81
- package/PR.md +0 -170
- package/PUBLISH.md +0 -77
- package/README.md +0 -108
- package/README_FRONTEND.md +0 -196
- package/REALIGNMENT/00-overview.md +0 -53
- package/REALIGNMENT/01-bug-list.md +0 -421
- package/REALIGNMENT/02-architecture.md +0 -322
- package/REALIGNMENT/03-phase-A-lockdown.md +0 -420
- package/REALIGNMENT/04-phase-B-live-zone.md +0 -470
- package/REALIGNMENT/05-phase-C-rust-proxy.md +0 -329
- package/REALIGNMENT/06-phase-D-bedrock-vertex.md +0 -222
- package/REALIGNMENT/07-phase-E-cache-stabilization.md +0 -338
- package/REALIGNMENT/08-phase-F-auth-mode.md +0 -254
- package/REALIGNMENT/09-phase-G-rtk-observability.md +0 -194
- package/REALIGNMENT/10-phase-H-python-retirement.md +0 -214
- package/REALIGNMENT/11-phase-I-test-infra.md +0 -385
- package/REALIGNMENT/12-decisions-needed.md +0 -196
- package/REALIGNMENT/INDEX.md +0 -82
- package/REFACTORING_CAMPAIGN.md +0 -277
- package/RESTRUCTURE_PLAN.md +0 -100
- package/RUST_DEV.md +0 -361
- package/SECURITY.md +0 -65
- package/SETUP_AUTO_PUBLISH.md +0 -164
- package/TESTING-copilot-subscription.md +0 -153
- package/app/api/config/route.ts +0 -22
- package/app/api/export/route.ts +0 -83
- package/app/api/models/route.ts +0 -12
- package/app/api/stats/route.ts +0 -46
- package/app/dashboard/page.tsx +0 -445
- package/app/globals.css +0 -35
- package/app/installation/page.tsx +0 -293
- package/app/layout.tsx +0 -35
- package/app/master-design-system.css +0 -2076
- package/app/master-design-system.source.css +0 -8
- package/app/page.tsx +0 -187
- package/app/rechtliches/agb/page.tsx +0 -165
- package/app/rechtliches/datenschutz/page.tsx +0 -128
- package/app/rechtliches/impressum/page.tsx +0 -122
- package/app/rechtliches/widerruf/page.tsx +0 -185
- package/codecov.yml +0 -34
- package/components/layout/AppShell.tsx +0 -25
- package/components/layout/Footer.tsx +0 -120
- package/components/layout/Header.tsx +0 -145
- package/components/marketing/LegalHighlights.tsx +0 -45
- package/components/marketing/LegalPageShell.tsx +0 -49
- package/components/marketing/LegalPanel.tsx +0 -41
- package/components/marketing/index.ts +0 -3
- package/crates/headroom-core/Cargo.toml +0 -165
- package/crates/headroom-core/benches/auth_mode.rs +0 -74
- package/crates/headroom-core/benches/ccr_store.rs +0 -224
- package/crates/headroom-core/benches/tokenizer.rs +0 -52
- package/crates/headroom-core/config/pipeline.toml +0 -151
- package/crates/headroom-core/proptest-regressions/transforms/tag_protector.txt +0 -9
- package/crates/headroom-core/src/auth_mode.rs +0 -250
- package/crates/headroom-core/src/cache_control.rs +0 -414
- package/crates/headroom-core/src/ccr/backends/in_memory.rs +0 -317
- package/crates/headroom-core/src/ccr/backends/mod.rs +0 -152
- package/crates/headroom-core/src/ccr/backends/redis.rs +0 -146
- package/crates/headroom-core/src/ccr/backends/sqlite.rs +0 -205
- package/crates/headroom-core/src/ccr/mod.rs +0 -119
- package/crates/headroom-core/src/compression_policy.rs +0 -505
- package/crates/headroom-core/src/lib.rs +0 -33
- package/crates/headroom-core/src/relevance/base.rs +0 -139
- package/crates/headroom-core/src/relevance/bm25.rs +0 -385
- package/crates/headroom-core/src/relevance/embedding.rs +0 -354
- package/crates/headroom-core/src/relevance/hybrid.rs +0 -364
- package/crates/headroom-core/src/relevance/mod.rs +0 -64
- package/crates/headroom-core/src/signals/keyword_detector.rs +0 -433
- package/crates/headroom-core/src/signals/line_importance.rs +0 -84
- package/crates/headroom-core/src/signals/mod.rs +0 -61
- package/crates/headroom-core/src/signals/tiered.rs +0 -141
- package/crates/headroom-core/src/tokenizer/estimator.rs +0 -152
- package/crates/headroom-core/src/tokenizer/hf_impl.rs +0 -314
- package/crates/headroom-core/src/tokenizer/mod.rs +0 -56
- package/crates/headroom-core/src/tokenizer/registry.rs +0 -342
- package/crates/headroom-core/src/tokenizer/tiktoken_impl.rs +0 -265
- package/crates/headroom-core/src/transforms/adaptive_sizer.rs +0 -610
- package/crates/headroom-core/src/transforms/anchor_selector.rs +0 -1189
- package/crates/headroom-core/src/transforms/content_detector.rs +0 -769
- package/crates/headroom-core/src/transforms/detection.rs +0 -222
- package/crates/headroom-core/src/transforms/diff_compressor.rs +0 -1685
- package/crates/headroom-core/src/transforms/live_zone.rs +0 -2967
- package/crates/headroom-core/src/transforms/log_compressor.rs +0 -1295
- package/crates/headroom-core/src/transforms/magika_detector.rs +0 -395
- package/crates/headroom-core/src/transforms/mod.rs +0 -64
- package/crates/headroom-core/src/transforms/pipeline/config.rs +0 -331
- package/crates/headroom-core/src/transforms/pipeline/mod.rs +0 -103
- package/crates/headroom-core/src/transforms/pipeline/offloads/diff_noise.rs +0 -524
- package/crates/headroom-core/src/transforms/pipeline/offloads/diff_offload.rs +0 -279
- package/crates/headroom-core/src/transforms/pipeline/offloads/json_offload.rs +0 -400
- package/crates/headroom-core/src/transforms/pipeline/offloads/log_offload.rs +0 -284
- package/crates/headroom-core/src/transforms/pipeline/offloads/mod.rs +0 -34
- package/crates/headroom-core/src/transforms/pipeline/offloads/search_offload.rs +0 -331
- package/crates/headroom-core/src/transforms/pipeline/orchestrator.rs +0 -848
- package/crates/headroom-core/src/transforms/pipeline/reformats/json_minifier.rs +0 -175
- package/crates/headroom-core/src/transforms/pipeline/reformats/log_template.rs +0 -545
- package/crates/headroom-core/src/transforms/pipeline/reformats/mod.rs +0 -12
- package/crates/headroom-core/src/transforms/pipeline/traits.rs +0 -338
- package/crates/headroom-core/src/transforms/recommendations.rs +0 -329
- package/crates/headroom-core/src/transforms/safety.rs +0 -215
- package/crates/headroom-core/src/transforms/search_compressor.rs +0 -902
- package/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs +0 -1262
- package/crates/headroom-core/src/transforms/smart_crusher/anchors.rs +0 -407
- package/crates/headroom-core/src/transforms/smart_crusher/builder.rs +0 -263
- package/crates/headroom-core/src/transforms/smart_crusher/classifier.rs +0 -214
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/classifier.rs +0 -283
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/compactor.rs +0 -733
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/formatter.rs +0 -961
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/ir.rs +0 -252
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/mod.rs +0 -127
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/walker.rs +0 -397
- package/crates/headroom-core/src/transforms/smart_crusher/config.rs +0 -173
- package/crates/headroom-core/src/transforms/smart_crusher/constraints.rs +0 -159
- package/crates/headroom-core/src/transforms/smart_crusher/crusher.rs +0 -1665
- package/crates/headroom-core/src/transforms/smart_crusher/crushers.rs +0 -865
- package/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs +0 -77
- package/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs +0 -393
- package/crates/headroom-core/src/transforms/smart_crusher/hashing.rs +0 -72
- package/crates/headroom-core/src/transforms/smart_crusher/mod.rs +0 -81
- package/crates/headroom-core/src/transforms/smart_crusher/observer.rs +0 -66
- package/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs +0 -459
- package/crates/headroom-core/src/transforms/smart_crusher/outliers.rs +0 -507
- package/crates/headroom-core/src/transforms/smart_crusher/planning.rs +0 -904
- package/crates/headroom-core/src/transforms/smart_crusher/statistics.rs +0 -510
- package/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs +0 -287
- package/crates/headroom-core/src/transforms/smart_crusher/traits.rs +0 -203
- package/crates/headroom-core/src/transforms/smart_crusher/types.rs +0 -263
- package/crates/headroom-core/src/transforms/tag_protector.rs +0 -1272
- package/crates/headroom-core/src/transforms/unidiff_detector.rs +0 -232
- package/crates/headroom-parity/Cargo.toml +0 -26
- package/crates/headroom-parity/examples/diff_fixture.rs +0 -66
- package/crates/headroom-parity/src/bin/parity_run.rs +0 -78
- package/crates/headroom-parity/src/lib.rs +0 -613
- package/crates/headroom-proxy/Cargo.toml +0 -119
- package/crates/headroom-proxy/data/model_prices_and_context_window.json +0 -39767
- package/crates/headroom-proxy/src/bedrock/auth_mode_layer.rs +0 -192
- package/crates/headroom-proxy/src/bedrock/envelope.rs +0 -239
- package/crates/headroom-proxy/src/bedrock/eventstream.rs +0 -769
- package/crates/headroom-proxy/src/bedrock/eventstream_to_sse.rs +0 -459
- package/crates/headroom-proxy/src/bedrock/invoke.rs +0 -735
- package/crates/headroom-proxy/src/bedrock/invoke_streaming.rs +0 -1098
- package/crates/headroom-proxy/src/bedrock/mod.rs +0 -66
- package/crates/headroom-proxy/src/bedrock/sigv4.rs +0 -324
- package/crates/headroom-proxy/src/bedrock/vendor.rs +0 -82
- package/crates/headroom-proxy/src/cache_stabilization/anthropic_cache_control.rs +0 -698
- package/crates/headroom-proxy/src/cache_stabilization/drift_detector.rs +0 -683
- package/crates/headroom-proxy/src/cache_stabilization/mod.rs +0 -61
- package/crates/headroom-proxy/src/cache_stabilization/openai_cache_key.rs +0 -600
- package/crates/headroom-proxy/src/cache_stabilization/tool_def_normalize.rs +0 -514
- package/crates/headroom-proxy/src/cache_stabilization/volatile_detector.rs +0 -720
- package/crates/headroom-proxy/src/compression/anthropic.rs +0 -92
- package/crates/headroom-proxy/src/compression/live_zone_anthropic.rs +0 -1311
- package/crates/headroom-proxy/src/compression/live_zone_openai.rs +0 -660
- package/crates/headroom-proxy/src/compression/live_zone_responses.rs +0 -678
- package/crates/headroom-proxy/src/compression/mod.rs +0 -127
- package/crates/headroom-proxy/src/compression/model_limits.rs +0 -211
- package/crates/headroom-proxy/src/config.rs +0 -646
- package/crates/headroom-proxy/src/error.rs +0 -69
- package/crates/headroom-proxy/src/handlers/chat_completions.rs +0 -100
- package/crates/headroom-proxy/src/handlers/conversations.rs +0 -241
- package/crates/headroom-proxy/src/handlers/mod.rs +0 -13
- package/crates/headroom-proxy/src/handlers/responses.rs +0 -256
- package/crates/headroom-proxy/src/headers.rs +0 -288
- package/crates/headroom-proxy/src/health.rs +0 -41
- package/crates/headroom-proxy/src/lib.rs +0 -21
- package/crates/headroom-proxy/src/main.rs +0 -141
- package/crates/headroom-proxy/src/observability/cache_hit_rate.rs +0 -340
- package/crates/headroom-proxy/src/observability/compression_ratio.rs +0 -158
- package/crates/headroom-proxy/src/observability/metric_names.rs +0 -174
- package/crates/headroom-proxy/src/observability/mod.rs +0 -68
- package/crates/headroom-proxy/src/observability/prometheus.rs +0 -394
- package/crates/headroom-proxy/src/observability/proxy_metrics.rs +0 -368
- package/crates/headroom-proxy/src/proxy.rs +0 -1619
- package/crates/headroom-proxy/src/responses_items.rs +0 -565
- package/crates/headroom-proxy/src/sse/anthropic.rs +0 -415
- package/crates/headroom-proxy/src/sse/framing.rs +0 -366
- package/crates/headroom-proxy/src/sse/mod.rs +0 -40
- package/crates/headroom-proxy/src/sse/openai_chat.rs +0 -231
- package/crates/headroom-proxy/src/sse/openai_responses.rs +0 -458
- package/crates/headroom-proxy/src/vertex/adc.rs +0 -315
- package/crates/headroom-proxy/src/vertex/envelope.rs +0 -167
- package/crates/headroom-proxy/src/vertex/mod.rs +0 -277
- package/crates/headroom-proxy/src/vertex/raw_predict.rs +0 -490
- package/crates/headroom-proxy/src/vertex/stream_raw_predict.rs +0 -49
- package/crates/headroom-proxy/src/websocket.rs +0 -246
- package/crates/headroom-py/Cargo.toml +0 -37
- package/crates/headroom-py/build.rs +0 -77
- package/crates/headroom-py/glibc_compat.c +0 -170
- package/crates/headroom-py/src/lib.rs +0 -1655
- package/deny.toml +0 -32
- package/design-system/base.css +0 -127
- package/design-system/components.css +0 -1943
- package/design-system/fonts.css +0 -17
- package/design-system/tailwind-preset.js +0 -95
- package/design-system/tokens.css +0 -48
- package/design-system/utilities.css +0 -53
- package/docker/differential-network-capture/Dockerfile.runner +0 -14
- package/docker/differential-network-capture/docker-compose.yml +0 -142
- package/docker/differential-network-capture/run-claude-lane.sh +0 -24
- package/docker/docker-compose.native.yml +0 -44
- package/docker-bake.hcl +0 -88
- package/docker-compose.yml +0 -80
- package/e2e/_lib/make_shim.ps1 +0 -24
- package/e2e/_lib/make_shim.sh +0 -28
- package/e2e/docker-native-install.sh +0 -120
- package/e2e/init/Dockerfile +0 -79
- package/e2e/wrap/Dockerfile +0 -115
- package/examples/07-context-compression.ipynb +0 -279
- package/examples/deployment/macos-launchagent/com.headroom.proxy.plist.template +0 -65
- package/examples/deployment/macos-launchagent/install.sh +0 -227
- package/examples/deployment/macos-launchagent/shell-integration.sh +0 -74
- package/examples/deployment/macos-launchagent/uninstall.sh +0 -145
- package/headroom-savings.png +0 -0
- package/headroom_learn.gif +0 -0
- package/lib/config/index.ts +0 -12
- package/lib/hooks/index.ts +0 -4
- package/lib/hooks/use-mounted.ts +0 -13
- package/lib/hooks/use-session.ts +0 -34
- package/lib/hooks/use-settings.ts +0 -49
- package/lib/utils/index.ts +0 -1
- package/lib/utils/settings.ts +0 -162
- package/lib/utils/utils.ts +0 -6
- package/llms.txt +0 -67
- package/middleware.ts +0 -26
- package/mkdocs.yml +0 -136
- package/next-env.d.ts +0 -7
- package/next.config.ts +0 -12
- package/npm/bin/xithead.js +0 -366
- package/npm/bin/xtream-head.js +0 -366
- package/npm/package.json +0 -55
- package/plugins/headroom-agent-hooks/.claude-plugin/plugin.json +0 -17
- package/plugins/headroom-agent-hooks/hooks/hooks.json +0 -29
- package/plugins/headroom-oauth2/LICENSE +0 -3
- package/plugins/headroom-oauth2/SPEC.md +0 -115
- package/plugins/headroom-oauth2/pyproject.toml +0 -48
- package/plugins/hermes/headroom_retrieve/plugin.yaml +0 -6
- package/plugins/openclaw/hook-shim/HOOK.md +0 -21
- package/plugins/openclaw/hook-shim/handler.js +0 -3
- package/plugins/openclaw/openclaw.plugin.json +0 -98
- package/plugins/openclaw/package-lock.json +0 -2692
- package/plugins/openclaw/package.json +0 -54
- package/plugins/openclaw/prepare-dist.mjs +0 -56
- package/plugins/openclaw/src/convert.ts +0 -418
- package/plugins/openclaw/src/engine.ts +0 -284
- package/plugins/openclaw/src/gateway-config.ts +0 -143
- package/plugins/openclaw/src/index.ts +0 -11
- package/plugins/openclaw/src/plugin/index.ts +0 -98
- package/plugins/openclaw/src/proxy-manager.ts +0 -477
- package/plugins/openclaw/src/tools/headroom-retrieve.ts +0 -76
- package/plugins/openclaw/test/convert.test.ts +0 -96
- package/plugins/openclaw/test/engine-normalization.test.ts +0 -21
- package/plugins/openclaw/test/engine.test.ts +0 -101
- package/plugins/openclaw/test/gateway-config.test.ts +0 -278
- package/plugins/openclaw/test/plugin-runtime-routing.test.ts +0 -141
- package/plugins/openclaw/test/proxy-manager.test.ts +0 -343
- package/plugins/openclaw/tsup.config.ts +0 -9
- package/postcss.config.mjs +0 -7
- package/pyproject.toml +0 -438
- package/rust-toolchain.toml +0 -16
- package/sdk/typescript/examples/basic-compress.ts +0 -64
- package/sdk/typescript/examples/ccr-retrieve.ts +0 -80
- package/sdk/typescript/examples/hooks-custom-compression.ts +0 -117
- package/sdk/typescript/examples/middleware-composition.ts +0 -59
- package/sdk/typescript/examples/multi-provider.ts +0 -53
- package/sdk/typescript/examples/openai-anthropic-adapters.ts +0 -73
- package/sdk/typescript/examples/shared-context-multi-agent.ts +0 -74
- package/sdk/typescript/examples/simulation-dry-run.ts +0 -80
- package/sdk/typescript/examples/streaming-chat.ts +0 -71
- package/sdk/typescript/examples/structured-output.ts +0 -99
- package/sdk/typescript/examples/tool-calling-agent.ts +0 -86
- package/sdk/typescript/examples/with-headroom-vercel.ts +0 -57
- package/sdk/typescript/package-lock.json +0 -3437
- package/sdk/typescript/package.json +0 -108
- package/sdk/typescript/src/adapters/anthropic.ts +0 -218
- package/sdk/typescript/src/adapters/gemini.ts +0 -83
- package/sdk/typescript/src/adapters/openai.ts +0 -77
- package/sdk/typescript/src/adapters/vercel-ai.ts +0 -131
- package/sdk/typescript/src/client.ts +0 -632
- package/sdk/typescript/src/compress.ts +0 -87
- package/sdk/typescript/src/errors.ts +0 -114
- package/sdk/typescript/src/hooks.ts +0 -113
- package/sdk/typescript/src/index.ts +0 -140
- package/sdk/typescript/src/paths.ts +0 -272
- package/sdk/typescript/src/shared-context.ts +0 -188
- package/sdk/typescript/src/simulate.ts +0 -52
- package/sdk/typescript/src/types/config.ts +0 -183
- package/sdk/typescript/src/types/models.ts +0 -314
- package/sdk/typescript/src/types.ts +0 -139
- package/sdk/typescript/src/utils/case.ts +0 -34
- package/sdk/typescript/src/utils/format.ts +0 -480
- package/sdk/typescript/src/utils/stream.ts +0 -50
- package/sdk/typescript/test/adapters/anthropic.test.ts +0 -217
- package/sdk/typescript/test/adapters/gemini.test.ts +0 -131
- package/sdk/typescript/test/adapters/openai.test.ts +0 -174
- package/sdk/typescript/test/adapters/vercel-ai.test.ts +0 -306
- package/sdk/typescript/test/client-expanded.test.ts +0 -333
- package/sdk/typescript/test/client.test.ts +0 -266
- package/sdk/typescript/test/compress.test.ts +0 -118
- package/sdk/typescript/test/config.test.ts +0 -275
- package/sdk/typescript/test/errors.test.ts +0 -169
- package/sdk/typescript/test/format-integration.test.ts +0 -252
- package/sdk/typescript/test/hooks.test.ts +0 -265
- package/sdk/typescript/test/integration.test.ts +0 -249
- package/sdk/typescript/test/mdx-examples.test.ts +0 -253
- package/sdk/typescript/test/models.test.ts +0 -318
- package/sdk/typescript/test/parity.test.ts +0 -337
- package/sdk/typescript/test/paths.test.ts +0 -447
- package/sdk/typescript/test/shared-context.test.ts +0 -245
- package/sdk/typescript/test/simulate.test.ts +0 -84
- package/sdk/typescript/test/types.test.ts +0 -244
- package/sdk/typescript/test/utils/case.test.ts +0 -129
- package/sdk/typescript/test/utils/detect-format.test.ts +0 -133
- package/sdk/typescript/test/utils/format.test.ts +0 -434
- package/sdk/typescript/test/vercel-ai-e2e.test.ts +0 -179
- package/sdk/typescript/tsup.config.ts +0 -17
- package/tailwind.master.config.cjs +0 -47
- package/types/api.ts +0 -12
- package/types/config.ts +0 -9
- package/types/index.ts +0 -2
- package/uv.lock +0 -6951
- package/xithead.egg-info/PKG-INFO +0 -22
- package/xithead.egg-info/SOURCES.txt +0 -16
- package/xithead.egg-info/dependency_links.txt +0 -1
- package/xithead.egg-info/entry_points.txt +0 -2
- package/xithead.egg-info/requires.txt +0 -10
- package/xithead.egg-info/top_level.txt +0 -2
- package/xithead.ps1 +0 -136
- package/xithead.sh +0 -129
- package/xithead_dist/pyproject.toml +0 -39
- package/xithead_dist/xithead.egg-info/PKG-INFO +0 -22
- package/xithead_dist/xithead.egg-info/SOURCES.txt +0 -16
- package/xithead_dist/xithead.egg-info/dependency_links.txt +0 -1
- package/xithead_dist/xithead.egg-info/entry_points.txt +0 -3
- package/xithead_dist/xithead.egg-info/requires.txt +0 -10
- package/xithead_dist/xithead.egg-info/top_level.txt +0 -2
- /package/{npm/index.d.ts → index.d.ts} +0 -0
- /package/{npm/postinstall.js → postinstall.js} +0 -0
|
@@ -1,2967 +0,0 @@
|
|
|
1
|
-
//! Live-zone block dispatcher — Phase B.
|
|
2
|
-
//!
|
|
3
|
-
//! # The mental model
|
|
4
|
-
//!
|
|
5
|
-
//! After Phase B PR-B1 retired the message-dropping machinery, all
|
|
6
|
-
//! compression happens *within* messages, never *between* them. The
|
|
7
|
-
//! live-zone dispatcher walks the request body and identifies the
|
|
8
|
-
//! *live zone*: the blocks the model will emit a response *against*,
|
|
9
|
-
//! which are the only ones whose bytes can mutate without busting the
|
|
10
|
-
//! provider's prompt cache.
|
|
11
|
-
//!
|
|
12
|
-
//! # Provider scope
|
|
13
|
-
//!
|
|
14
|
-
//! Phase B ships ONE dispatcher entry point —
|
|
15
|
-
//! [`compress_anthropic_live_zone`] — that handles the Anthropic
|
|
16
|
-
//! Messages API shape (`/v1/messages`). Other providers (OpenAI
|
|
17
|
-
//! Chat Completions, OpenAI Responses, Google Gemini, Bedrock with
|
|
18
|
-
//! native payloads, …) need their own dispatchers because their
|
|
19
|
-
//! request shapes diverge in load-bearing ways:
|
|
20
|
-
//!
|
|
21
|
-
//! - OpenAI Chat Completions puts tool results in their own
|
|
22
|
-
//! `role: "tool"` messages, not nested in user messages.
|
|
23
|
-
//! - OpenAI Responses uses `input` (not `messages`) with item types
|
|
24
|
-
//! like `function_call_output` and `reasoning`.
|
|
25
|
-
//! - Gemini uses `contents`/`parts`/`function_response`.
|
|
26
|
-
//!
|
|
27
|
-
//! Phase C (`REALIGNMENT/05-phase-C-rust-proxy.md`) introduces
|
|
28
|
-
//! `compress_openai_chat_live_zone`, `compress_openai_responses_live_zone`,
|
|
29
|
-
//! and friends. They share this module's provider-agnostic types
|
|
30
|
-
//! ([`LiveZoneOutcome`], [`BlockAction`], [`CompressionManifest`])
|
|
31
|
-
//! and the per-content-type compressor backend, but each owns its
|
|
32
|
-
//! own walker.
|
|
33
|
-
//!
|
|
34
|
-
//! For Anthropic `/v1/messages`, the live zone is bounded by:
|
|
35
|
-
//!
|
|
36
|
-
//! - **Floor:** `frozen_message_count` (computed by
|
|
37
|
-
//! [`crate::compute_frozen_count`] from explicit `cache_control`
|
|
38
|
-
//! markers; passed in here). Indices below the floor are in the
|
|
39
|
-
//! prompt cache and MUST be byte-identical.
|
|
40
|
-
//! - **Ceiling:** the latest user message. The latest assistant
|
|
41
|
-
//! message (if any) is part of the cache hot zone too — it's what
|
|
42
|
-
//! the next response continues from. We never touch it.
|
|
43
|
-
//! - **Inside the latest user message:** every block is a candidate.
|
|
44
|
-
//! The most common compressible block type is `tool_result`
|
|
45
|
-
//! (because tool outputs dominate token budgets); `text` blocks
|
|
46
|
-
//! are also eligible (e.g. user pastes a long log).
|
|
47
|
-
//!
|
|
48
|
-
//! # Phase B build-up
|
|
49
|
-
//!
|
|
50
|
-
//! - **PR-B2** shipped the dispatcher *skeleton*: identify live-zone
|
|
51
|
-
//! blocks, route to no-op compressors, always return `NoChange`.
|
|
52
|
-
//! - **PR-B3** (this PR) wires per-content-type compressors:
|
|
53
|
-
//! `JsonArray` → SmartCrusher; `BuildOutput` → LogCompressor;
|
|
54
|
-
//! `SearchResults` → SearchCompressor; `GitDiff` → DiffCompressor;
|
|
55
|
-
//! `SourceCode` / `PlainText` / `Html` → no-op (B4 + a Rust
|
|
56
|
-
//! code-compressor port follow-up).
|
|
57
|
-
//! - **PR-B4** adds the tokenizer-validation gate (per-block
|
|
58
|
-
//! `compressed.tokens >= original.tokens` → fall back) and the
|
|
59
|
-
//! per-content-type byte threshold below which compression is
|
|
60
|
-
//! skipped.
|
|
61
|
-
//! - **PR-B7** wires CCR retrieval-marker injection.
|
|
62
|
-
//!
|
|
63
|
-
//! # Cache safety invariant
|
|
64
|
-
//!
|
|
65
|
-
//! Bytes outside the live zone are NEVER touched. PR-B3 writes new
|
|
66
|
-
//! bodies via **byte-range surgery**: we locate each rewritten block
|
|
67
|
-
//! by pointer arithmetic on `serde_json::value::RawValue` borrowed
|
|
68
|
-
//! slices (which retain their offset into the original buffer), then
|
|
69
|
-
//! splice the replacement into the output. Concretely:
|
|
70
|
-
//!
|
|
71
|
-
//! ```text
|
|
72
|
-
//! out = body[..block_start] || replacement || body[block_end..]
|
|
73
|
-
//! ```
|
|
74
|
-
//!
|
|
75
|
-
//! The bytes outside the rewritten ranges are *literally copied*
|
|
76
|
-
//! from the input, never re-serialized. This is how we guarantee
|
|
77
|
-
//! the SHA-256 of the prefix and suffix are byte-identical to the
|
|
78
|
-
//! input — Phase A's fixtures and B3's `byte_fidelity_outside_compressed_block`
|
|
79
|
-
//! test pin this in CI.
|
|
80
|
-
//!
|
|
81
|
-
//! Why byte-range surgery and not "deserialize → mutate → serialize"?
|
|
82
|
-
//! Re-serializing a JSON `Value` does not preserve original
|
|
83
|
-
//! whitespace, key order subtleties, or numeric formatting that the
|
|
84
|
-
//! provider may have already cached against. Byte-faithful copy of
|
|
85
|
-
//! everything we don't touch is the only way to guarantee
|
|
86
|
-
//! cache stability — see `project_compression_realignment_2026_05`.
|
|
87
|
-
//!
|
|
88
|
-
//! # AuthMode
|
|
89
|
-
//!
|
|
90
|
-
//! The `AuthMode` parameter is taken in B3 but unused — Phase F
|
|
91
|
-
//! PR-F2 wires the gate (PAYG/OAuth/Subscription each demand
|
|
92
|
-
//! different policies; see project memory
|
|
93
|
-
//! `project_auth_mode_compression_nuances.md`). Keeping the
|
|
94
|
-
//! parameter in the signature now means later PRs are pure
|
|
95
|
-
//! implementation swaps, not signature redesigns.
|
|
96
|
-
|
|
97
|
-
use std::{collections::HashSet, sync::OnceLock};
|
|
98
|
-
|
|
99
|
-
use serde::Deserialize;
|
|
100
|
-
use serde_json::value::RawValue;
|
|
101
|
-
use serde_json::Value;
|
|
102
|
-
use thiserror::Error;
|
|
103
|
-
|
|
104
|
-
use super::content_detector::{detect_content_type, ContentType};
|
|
105
|
-
use super::diff_compressor::{DiffCompressor, DiffCompressorConfig};
|
|
106
|
-
use super::log_compressor::{LogCompressor, LogCompressorConfig};
|
|
107
|
-
use super::search_compressor::{SearchCompressor, SearchCompressorConfig};
|
|
108
|
-
use super::smart_crusher::{SmartCrusher, SmartCrusherConfig};
|
|
109
|
-
use crate::ccr::{compute_key, marker_for, CcrStore};
|
|
110
|
-
use crate::tokenizer::get_tokenizer;
|
|
111
|
-
|
|
112
|
-
// ─── Tunable constants (no magic numbers in the dispatch logic) ────────
|
|
113
|
-
|
|
114
|
-
/// Strategy tag emitted when SmartCrusher rewrote a JSON-array block.
|
|
115
|
-
const STRATEGY_SMART_CRUSHER: &str = "smart_crusher";
|
|
116
|
-
/// Strategy tag emitted when LogCompressor rewrote a build-output / log block.
|
|
117
|
-
const STRATEGY_LOG_COMPRESSOR: &str = "log_compressor";
|
|
118
|
-
/// Strategy tag emitted when SearchCompressor rewrote a grep / ripgrep block.
|
|
119
|
-
const STRATEGY_SEARCH_COMPRESSOR: &str = "search_compressor";
|
|
120
|
-
/// Strategy tag emitted when DiffCompressor rewrote a unified-diff block.
|
|
121
|
-
const STRATEGY_DIFF_COMPRESSOR: &str = "diff_compressor";
|
|
122
|
-
|
|
123
|
-
/// Empty query context passed to compressors that take a relevance
|
|
124
|
-
/// query string. PR-B3 dispatcher does not yet plumb the user's last
|
|
125
|
-
/// prompt through; PR-F3 will.
|
|
126
|
-
const EMPTY_QUERY: &str = "";
|
|
127
|
-
/// Default relevance bias passed to scoring-aware compressors. Mirrors
|
|
128
|
-
/// the OSS-default behaviour ("no bias").
|
|
129
|
-
const DEFAULT_BIAS: f64 = 0.0;
|
|
130
|
-
|
|
131
|
-
/// Default model name handed to the tokenizer registry when the proxy
|
|
132
|
-
/// could not extract `body["model"]`. Matches the most-common
|
|
133
|
-
/// production Claude model — chars-per-token estimator for `claude-*`
|
|
134
|
-
/// is calibrated to 3.5 cpt; using a non-Claude model here would
|
|
135
|
-
/// silently pick a different estimator density. PR-F3 will plumb the
|
|
136
|
-
/// actual model from `body["model"]`; PR-B4 just establishes the
|
|
137
|
-
/// signature.
|
|
138
|
-
pub const DEFAULT_MODEL: &str = "claude-3-5-sonnet-20241022";
|
|
139
|
-
|
|
140
|
-
// ─── Per-content-type byte thresholds ──────────────────────────────────
|
|
141
|
-
//
|
|
142
|
-
// Below these byte sizes the dispatcher does not even attempt
|
|
143
|
-
// compression — the per-block overhead (tokenizer count, dispatcher
|
|
144
|
-
// bookkeeping, log lines) costs more than the marginal token savings,
|
|
145
|
-
// and tiny inputs almost never compress at all.
|
|
146
|
-
//
|
|
147
|
-
// Sourced from the spec (`REALIGNMENT/04-phase-B-live-zone.md::PR-B4`).
|
|
148
|
-
// Pinned as `const` rather than a hard-coded `match` so the values are
|
|
149
|
-
// grep-able and reviewable in one place.
|
|
150
|
-
|
|
151
|
-
/// JSON-array tool_results below this size route to no-op.
|
|
152
|
-
const THRESHOLD_JSON_ARRAY: usize = 512;
|
|
153
|
-
/// Build / log output below this size routes to no-op (512 B). Logs
|
|
154
|
-
/// are the most repetitive content type so the threshold is the
|
|
155
|
-
/// lowest of the bunch.
|
|
156
|
-
const THRESHOLD_BUILD_OUTPUT: usize = 512;
|
|
157
|
-
/// Search-result blocks below this size route to no-op.
|
|
158
|
-
const THRESHOLD_SEARCH_RESULTS: usize = 512;
|
|
159
|
-
/// Git-diff blocks below this size route to no-op.
|
|
160
|
-
const THRESHOLD_GIT_DIFF: usize = 512;
|
|
161
|
-
/// Source-code blocks below this size route to no-op. Pinned
|
|
162
|
-
/// for the future Rust code-compressor port — currently unused
|
|
163
|
-
/// because `ContentType::SourceCode` short-circuits to no-op above
|
|
164
|
-
/// the dispatch (see `dispatch_compressor`).
|
|
165
|
-
const THRESHOLD_SOURCE_CODE: usize = 512;
|
|
166
|
-
/// Plain-text blocks below this size route to no-op. Pinned
|
|
167
|
-
/// for the future Kompress wiring (PR-B7 follow-up); currently unused.
|
|
168
|
-
const THRESHOLD_PLAIN_TEXT: usize = 512;
|
|
169
|
-
/// HTML blocks have no compressor; threshold matches plain text so
|
|
170
|
-
/// when an HTML compressor lands the value is already pinned.
|
|
171
|
-
const THRESHOLD_HTML: usize = 512;
|
|
172
|
-
|
|
173
|
-
/// Map a content type to its byte threshold. Returning `usize` rather
|
|
174
|
-
/// than an `Option` because every variant has a sensible default;
|
|
175
|
-
/// `Html` is a no-op anyway so the threshold check never fires.
|
|
176
|
-
fn threshold_for(content_type: ContentType) -> usize {
|
|
177
|
-
match content_type {
|
|
178
|
-
ContentType::JsonArray => THRESHOLD_JSON_ARRAY,
|
|
179
|
-
ContentType::BuildOutput => THRESHOLD_BUILD_OUTPUT,
|
|
180
|
-
ContentType::SearchResults => THRESHOLD_SEARCH_RESULTS,
|
|
181
|
-
ContentType::GitDiff => THRESHOLD_GIT_DIFF,
|
|
182
|
-
ContentType::SourceCode => THRESHOLD_SOURCE_CODE,
|
|
183
|
-
ContentType::PlainText => THRESHOLD_PLAIN_TEXT,
|
|
184
|
-
ContentType::Html => THRESHOLD_HTML,
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// ─── Public types ──────────────────────────────────────────────────────
|
|
189
|
-
|
|
190
|
-
/// Authentication mode of the originating request. Passed through to
|
|
191
|
-
/// the dispatcher so PR-F2 can vary policy without re-shaping the
|
|
192
|
-
/// public API. PR-B3 ignores the value (always treated as `Payg`).
|
|
193
|
-
///
|
|
194
|
-
/// Also reused by [`super::recommendations`] (PR-B5) as the lookup
|
|
195
|
-
/// key prefix — keeping one canonical enum avoids drift between the
|
|
196
|
-
/// dispatcher's auth slice and the published recommendations'.
|
|
197
|
-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
198
|
-
pub enum AuthMode {
|
|
199
|
-
/// Pay-as-you-go API key. Most aggressive compression budget —
|
|
200
|
-
/// every saved token is real money for the customer.
|
|
201
|
-
Payg,
|
|
202
|
-
/// OAuth-bearing client (e.g. Anthropic.com OAuth). Compression
|
|
203
|
-
/// must not break the per-account routing the OAuth header pins;
|
|
204
|
-
/// otherwise behaves like PAYG.
|
|
205
|
-
OAuth,
|
|
206
|
-
/// Subscription seat (e.g. Claude.ai usage). The provider
|
|
207
|
-
/// already counts tokens against a fixed quota; aggressive
|
|
208
|
-
/// compression is less compelling and may interact badly with
|
|
209
|
-
/// rate-limit accounting.
|
|
210
|
-
Subscription,
|
|
211
|
-
/// Auth slice not yet detected. Matches the Python TOIN publish
|
|
212
|
-
/// CLI's "unknown" default. Used by the recommendations loader
|
|
213
|
-
/// (PR-B5) when an aggregation row didn't carry an auth tag.
|
|
214
|
-
Unknown,
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
impl AuthMode {
|
|
218
|
-
/// String form used as the recommendations-store lookup key.
|
|
219
|
-
/// Mirrors the Python publish CLI tag values.
|
|
220
|
-
pub fn as_str(self) -> &'static str {
|
|
221
|
-
match self {
|
|
222
|
-
AuthMode::Payg => "payg",
|
|
223
|
-
AuthMode::OAuth => "oauth",
|
|
224
|
-
AuthMode::Subscription => "subscription",
|
|
225
|
-
AuthMode::Unknown => "unknown",
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/// Map F1's classifier output (`crate::auth_mode::AuthMode`) to the
|
|
231
|
-
/// dispatcher-local enum. The two enums differ only by `Unknown` (the
|
|
232
|
-
/// dispatcher carries a sentinel for the case where a stored
|
|
233
|
-
/// recommendation row didn't include an auth tag); F1 always returns
|
|
234
|
-
/// one of the three real classes, so this `From` is total and
|
|
235
|
-
/// infallible.
|
|
236
|
-
impl From<crate::auth_mode::AuthMode> for AuthMode {
|
|
237
|
-
fn from(mode: crate::auth_mode::AuthMode) -> Self {
|
|
238
|
-
match mode {
|
|
239
|
-
crate::auth_mode::AuthMode::Payg => AuthMode::Payg,
|
|
240
|
-
crate::auth_mode::AuthMode::OAuth => AuthMode::OAuth,
|
|
241
|
-
crate::auth_mode::AuthMode::Subscription => AuthMode::Subscription,
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/// Per-block decision recorded for observability. Independent of
|
|
247
|
-
/// whether the body was actually rewritten.
|
|
248
|
-
#[derive(Debug, Clone)]
|
|
249
|
-
pub struct BlockOutcome {
|
|
250
|
-
/// Index into the `messages` array.
|
|
251
|
-
pub message_index: usize,
|
|
252
|
-
/// Index into the message's `content` array. `None` when the
|
|
253
|
-
/// content is a plain string (Anthropic accepts both shapes).
|
|
254
|
-
pub block_index: Option<usize>,
|
|
255
|
-
/// Block kind detected on this slot. `text`, `tool_result`,
|
|
256
|
-
/// `tool_use`, `image`, ... or `string_content` for the
|
|
257
|
-
/// string-shaped fallback.
|
|
258
|
-
pub block_type: String,
|
|
259
|
-
/// What the dispatcher decided.
|
|
260
|
-
pub action: BlockAction,
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/// Disposition of one block.
|
|
264
|
-
#[derive(Debug, Clone)]
|
|
265
|
-
pub enum BlockAction {
|
|
266
|
-
/// Content type was inspected, no compressor was applicable.
|
|
267
|
-
/// Examples: `PlainText` (Kompress wires in PR-B4), `SourceCode`
|
|
268
|
-
/// (Rust code-compressor port pending), `Html` (no compressor),
|
|
269
|
-
/// `Image` (binary), unknown shapes.
|
|
270
|
-
NoCompressionApplied {
|
|
271
|
-
/// String form of the detected content type — `"text"`,
|
|
272
|
-
/// `"source_code"`, `"html"`, `"image"`, `"unknown"`, etc.
|
|
273
|
-
content_type: String,
|
|
274
|
-
},
|
|
275
|
-
/// A compressor ran and produced a smaller output (in tokens, as
|
|
276
|
-
/// counted by the model's tokenizer) that was spliced into the
|
|
277
|
-
/// body. Both byte and token counts are reported so the proxy
|
|
278
|
-
/// can log the savings ratio in either currency.
|
|
279
|
-
Compressed {
|
|
280
|
-
/// Identifier of the compressor (`"smart_crusher"`,
|
|
281
|
-
/// `"log_compressor"`, ...). Static so the manifest is
|
|
282
|
-
/// allocation-light.
|
|
283
|
-
strategy: &'static str,
|
|
284
|
-
/// Bytes of the original block content (the JSON string
|
|
285
|
-
/// value, after unescaping).
|
|
286
|
-
original_bytes: usize,
|
|
287
|
-
/// Bytes of the replacement block content.
|
|
288
|
-
compressed_bytes: usize,
|
|
289
|
-
/// Tokens in the original block content (per the model's
|
|
290
|
-
/// tokenizer).
|
|
291
|
-
original_tokens: usize,
|
|
292
|
-
/// Tokens in the replacement block content. Always strictly
|
|
293
|
-
/// less than `original_tokens` for this variant — the
|
|
294
|
-
/// tokenizer-validated rejection gate (PR-B4) maps the
|
|
295
|
-
/// `>=` case to `RejectedNotSmaller`.
|
|
296
|
-
compressed_tokens: usize,
|
|
297
|
-
},
|
|
298
|
-
/// A compressor was tried but failed loudly. Per project memory
|
|
299
|
-
/// `feedback_no_silent_fallbacks.md`: surface the error in the
|
|
300
|
-
/// manifest; the proxy logs warn-level and forwards the original
|
|
301
|
-
/// bytes for that block (other blocks in the same body still get
|
|
302
|
-
/// compressed normally).
|
|
303
|
-
CompressorError {
|
|
304
|
-
/// Identifier of the compressor that failed.
|
|
305
|
-
strategy: &'static str,
|
|
306
|
-
/// Human-readable error string (from `Display`).
|
|
307
|
-
error: String,
|
|
308
|
-
},
|
|
309
|
-
/// A compressor ran but produced output that did not shrink the
|
|
310
|
-
/// token count. Cache safety + "don't make it worse" → keep the
|
|
311
|
-
/// original. PR-B4 wired the tokenizer-validated check; both
|
|
312
|
-
/// byte and token counts are reported for observability.
|
|
313
|
-
RejectedNotSmaller {
|
|
314
|
-
/// Identifier of the compressor that was rejected.
|
|
315
|
-
strategy: &'static str,
|
|
316
|
-
/// Original block-content size, bytes.
|
|
317
|
-
original_bytes: usize,
|
|
318
|
-
/// Would-be compressed-block-content size, bytes.
|
|
319
|
-
compressed_bytes: usize,
|
|
320
|
-
/// Original block-content size, tokens.
|
|
321
|
-
original_tokens: usize,
|
|
322
|
-
/// Would-be compressed-block-content size, tokens. Always
|
|
323
|
-
/// `>= original_tokens` (otherwise this would be
|
|
324
|
-
/// `Compressed`).
|
|
325
|
-
compressed_tokens: usize,
|
|
326
|
-
},
|
|
327
|
-
/// The block content was below the per-content-type byte
|
|
328
|
-
/// threshold; no compressor was invoked. The dispatcher does
|
|
329
|
-
/// not even spin up the tokenizer for these — they're below the
|
|
330
|
-
/// per-call overhead so the marginal savings are negative.
|
|
331
|
-
BelowByteThreshold {
|
|
332
|
-
/// Detected content type — string tag matches
|
|
333
|
-
/// `ContentType::as_str`.
|
|
334
|
-
content_type: &'static str,
|
|
335
|
-
/// Bytes in the block content.
|
|
336
|
-
byte_count: usize,
|
|
337
|
-
/// Threshold (in bytes) the content failed to clear.
|
|
338
|
-
threshold_bytes: usize,
|
|
339
|
-
},
|
|
340
|
-
/// Block type is intentionally outside the live zone (e.g.
|
|
341
|
-
/// `tool_use` → cache hot zone) and is excluded from dispatch.
|
|
342
|
-
Excluded { reason: ExclusionReason },
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/// Why a block was not eligible for compression.
|
|
346
|
-
#[derive(Debug, Clone, Copy)]
|
|
347
|
-
pub enum ExclusionReason {
|
|
348
|
-
/// Block is in a message at index `< frozen_message_count`.
|
|
349
|
-
BelowFrozenFloor,
|
|
350
|
-
/// Block belongs to a message above the latest user message
|
|
351
|
-
/// boundary (e.g. an older assistant turn).
|
|
352
|
-
AboveLiveZone,
|
|
353
|
-
/// Block type is on the cache-hot list (e.g. `tool_use`,
|
|
354
|
-
/// `thinking`, `redacted_thinking`).
|
|
355
|
-
HotZoneBlockType,
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/// Aggregated per-request manifest. Always populated, regardless of
|
|
359
|
-
/// whether any bytes were written.
|
|
360
|
-
#[derive(Debug, Clone)]
|
|
361
|
-
pub struct CompressionManifest {
|
|
362
|
-
/// Total messages in the input array. Matches
|
|
363
|
-
/// `body.messages.len()`.
|
|
364
|
-
pub messages_total: usize,
|
|
365
|
-
/// Messages with index `< frozen_message_count`. Untouched.
|
|
366
|
-
pub messages_below_frozen_floor: usize,
|
|
367
|
-
/// Index of the latest user message in the live zone, if any.
|
|
368
|
-
pub latest_user_message_index: Option<usize>,
|
|
369
|
-
/// Per-block outcomes for the latest user message. Empty when
|
|
370
|
-
/// the live zone has no eligible blocks (or the body has no
|
|
371
|
-
/// messages).
|
|
372
|
-
pub block_outcomes: Vec<BlockOutcome>,
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
impl CompressionManifest {
|
|
376
|
-
fn empty() -> Self {
|
|
377
|
-
Self {
|
|
378
|
-
messages_total: 0,
|
|
379
|
-
messages_below_frozen_floor: 0,
|
|
380
|
-
latest_user_message_index: None,
|
|
381
|
-
block_outcomes: Vec::new(),
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/// True when at least one block was actually rewritten by a
|
|
386
|
-
/// compressor (used to discriminate the `Modified` arm from
|
|
387
|
-
/// `NoChange`).
|
|
388
|
-
fn has_compressed_block(&self) -> bool {
|
|
389
|
-
self.block_outcomes
|
|
390
|
-
.iter()
|
|
391
|
-
.any(|b| matches!(b.action, BlockAction::Compressed { .. }))
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/// Aggregate `original_tokens − compressed_tokens` across every
|
|
395
|
-
/// `BlockAction::Compressed` outcome. Zero when no block was
|
|
396
|
-
/// rewritten. Saturating subtraction guards against the
|
|
397
|
-
/// theoretically-impossible case where a `Compressed` variant
|
|
398
|
-
/// reports compressed > original (the dispatcher's
|
|
399
|
-
/// `RejectedNotSmaller` gate should make this unreachable, but the
|
|
400
|
-
/// saturating arithmetic keeps callers panic-free).
|
|
401
|
-
pub fn tokens_saved(&self) -> usize {
|
|
402
|
-
self.block_outcomes
|
|
403
|
-
.iter()
|
|
404
|
-
.filter_map(|b| match &b.action {
|
|
405
|
-
BlockAction::Compressed {
|
|
406
|
-
original_tokens,
|
|
407
|
-
compressed_tokens,
|
|
408
|
-
..
|
|
409
|
-
} => Some(original_tokens.saturating_sub(*compressed_tokens)),
|
|
410
|
-
_ => None,
|
|
411
|
-
})
|
|
412
|
-
.sum()
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/// Distinct compressor strategies that actually produced rewritten
|
|
416
|
-
/// output, in first-seen order. Mirrors what the proxy logs as
|
|
417
|
-
/// `transforms_applied`. Empty when no block was rewritten.
|
|
418
|
-
pub fn transforms_applied(&self) -> Vec<&'static str> {
|
|
419
|
-
let mut seen: Vec<&'static str> = Vec::new();
|
|
420
|
-
for b in &self.block_outcomes {
|
|
421
|
-
if let BlockAction::Compressed { strategy, .. } = &b.action {
|
|
422
|
-
if !seen.contains(strategy) {
|
|
423
|
-
seen.push(*strategy);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
seen
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/// Summarize why a Responses live-zone dispatch made no changes.
|
|
432
|
-
///
|
|
433
|
-
/// The proxy uses this to log stable, grep-able reasons instead of the
|
|
434
|
-
/// generic `rust_no_compression` bucket. The classification is
|
|
435
|
-
/// intentionally coarse: operators want to know whether the dispatcher
|
|
436
|
-
/// saw no eligible items, hit a size floor, rejected output as not
|
|
437
|
-
/// smaller, or encountered a compressor error.
|
|
438
|
-
pub fn summarize_openai_responses_no_change_reason(manifest: &CompressionManifest) -> &'static str {
|
|
439
|
-
if manifest.block_outcomes.is_empty() {
|
|
440
|
-
return "no_eligible_items";
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
let mut saw_no_compression_applied = false;
|
|
444
|
-
let mut saw_excluded = false;
|
|
445
|
-
let mut saw_below_output_floor = false;
|
|
446
|
-
let mut saw_below_plain_text_floor = false;
|
|
447
|
-
let mut saw_rejected_not_smaller = false;
|
|
448
|
-
let mut saw_compressor_error = false;
|
|
449
|
-
|
|
450
|
-
for outcome in &manifest.block_outcomes {
|
|
451
|
-
match &outcome.action {
|
|
452
|
-
BlockAction::CompressorError { .. } => saw_compressor_error = true,
|
|
453
|
-
BlockAction::RejectedNotSmaller { .. } => saw_rejected_not_smaller = true,
|
|
454
|
-
BlockAction::BelowByteThreshold { content_type, .. } => {
|
|
455
|
-
if *content_type == "output_item" {
|
|
456
|
-
saw_below_output_floor = true;
|
|
457
|
-
} else {
|
|
458
|
-
saw_below_plain_text_floor = true;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
BlockAction::NoCompressionApplied { .. } => saw_no_compression_applied = true,
|
|
462
|
-
BlockAction::Excluded { .. } => saw_excluded = true,
|
|
463
|
-
BlockAction::Compressed { .. } => {}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
if saw_compressor_error {
|
|
468
|
-
"compressor_error"
|
|
469
|
-
} else if saw_rejected_not_smaller {
|
|
470
|
-
"rejected_not_smaller"
|
|
471
|
-
} else if saw_below_output_floor {
|
|
472
|
-
"below_output_floor"
|
|
473
|
-
} else if saw_below_plain_text_floor {
|
|
474
|
-
"below_plain_text_floor"
|
|
475
|
-
} else if saw_excluded {
|
|
476
|
-
"excluded_live_zone"
|
|
477
|
-
} else if saw_no_compression_applied {
|
|
478
|
-
"no_compressible_content"
|
|
479
|
-
} else {
|
|
480
|
-
"no_change"
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/// Outcome of dispatching the live zone.
|
|
485
|
-
#[derive(Debug)]
|
|
486
|
-
pub enum LiveZoneOutcome {
|
|
487
|
-
/// No bytes were rewritten. The caller must forward the original
|
|
488
|
-
/// buffered request body byte-for-byte.
|
|
489
|
-
NoChange { manifest: CompressionManifest },
|
|
490
|
-
/// The dispatcher rewrote at least one block and emitted a fresh
|
|
491
|
-
/// body. The caller forwards `new_body` upstream.
|
|
492
|
-
Modified {
|
|
493
|
-
new_body: Box<RawValue>,
|
|
494
|
-
manifest: CompressionManifest,
|
|
495
|
-
},
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/// Dispatcher errors. Every variant is recoverable by the caller —
|
|
499
|
-
/// the proxy turns each into a structured warn-level log and
|
|
500
|
-
/// falls back to forwarding the original bytes.
|
|
501
|
-
#[derive(Debug, Error)]
|
|
502
|
-
pub enum LiveZoneError {
|
|
503
|
-
/// The request body is not valid JSON.
|
|
504
|
-
#[error("request body is not valid JSON: {0}")]
|
|
505
|
-
BodyNotJson(serde_json::Error),
|
|
506
|
-
/// `messages` field is missing or not a JSON array.
|
|
507
|
-
#[error("body has no `messages` array")]
|
|
508
|
-
NoMessagesArray,
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
/// Block types the live-zone dispatcher considers "in the cache hot
|
|
512
|
-
/// zone" even when they appear inside a live-zone message. Listed
|
|
513
|
-
/// explicitly (no string-prefix matching) so the cache-safety
|
|
514
|
-
/// surface is grep-able.
|
|
515
|
-
const HOT_ZONE_BLOCK_TYPES: &[&str] = &[
|
|
516
|
-
"tool_use",
|
|
517
|
-
"thinking",
|
|
518
|
-
"redacted_thinking",
|
|
519
|
-
// Anthropic compaction items — once injected they're sticky to
|
|
520
|
-
// the cache as much as `tool_use` is.
|
|
521
|
-
"compaction",
|
|
522
|
-
];
|
|
523
|
-
|
|
524
|
-
// ─── Compressor singletons ─────────────────────────────────────────────
|
|
525
|
-
//
|
|
526
|
-
// Each compressor's struct holds its config + (for SmartCrusher) the
|
|
527
|
-
// scoring infrastructure. Allocating one per request would be
|
|
528
|
-
// wasteful and (in SmartCrusher's case) defeats the purpose of the
|
|
529
|
-
// builder. Hold one instance per process behind `OnceLock`; cheap to
|
|
530
|
-
// clone the &reference each call.
|
|
531
|
-
|
|
532
|
-
fn smart_crusher() -> &'static SmartCrusher {
|
|
533
|
-
static INSTANCE: OnceLock<SmartCrusher> = OnceLock::new();
|
|
534
|
-
INSTANCE.get_or_init(|| SmartCrusher::new(SmartCrusherConfig::default()))
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
fn log_compressor() -> &'static LogCompressor {
|
|
538
|
-
static INSTANCE: OnceLock<LogCompressor> = OnceLock::new();
|
|
539
|
-
INSTANCE.get_or_init(|| LogCompressor::new(LogCompressorConfig::default()))
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
fn search_compressor() -> &'static SearchCompressor {
|
|
543
|
-
static INSTANCE: OnceLock<SearchCompressor> = OnceLock::new();
|
|
544
|
-
INSTANCE.get_or_init(|| SearchCompressor::new(SearchCompressorConfig::default()))
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
fn diff_compressor() -> &'static DiffCompressor {
|
|
548
|
-
static INSTANCE: OnceLock<DiffCompressor> = OnceLock::new();
|
|
549
|
-
INSTANCE.get_or_init(|| DiffCompressor::new(DiffCompressorConfig::default()))
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
// ─── Public entry point ────────────────────────────────────────────────
|
|
553
|
-
|
|
554
|
-
/// Inspect a buffered Anthropic `/v1/messages` body and decide which
|
|
555
|
-
/// blocks (if any) to rewrite.
|
|
556
|
-
///
|
|
557
|
-
/// # Provider scope (Phase B)
|
|
558
|
-
///
|
|
559
|
-
/// This function only handles the Anthropic Messages API shape:
|
|
560
|
-
///
|
|
561
|
-
/// - `messages: [{role, content}]`, with `content` either a JSON
|
|
562
|
-
/// string or an array of typed blocks (`text`, `tool_result`,
|
|
563
|
-
/// `tool_use`, `thinking`, `image`, …).
|
|
564
|
-
/// - The "live zone" is the latest `role == "user"` message at or
|
|
565
|
-
/// above `frozen_message_count`. Earlier messages are in the
|
|
566
|
-
/// prompt cache hot zone and are byte-preserved.
|
|
567
|
-
///
|
|
568
|
-
/// **Other providers need their own dispatchers** because their
|
|
569
|
-
/// request shapes diverge:
|
|
570
|
-
///
|
|
571
|
-
/// - **OpenAI Chat Completions** (`/v1/chat/completions`) — tool
|
|
572
|
-
/// results live in their own `role: "tool"` messages, not nested
|
|
573
|
-
/// in user messages. The live zone is the trailing run of
|
|
574
|
-
/// `tool` messages plus the latest `user` message.
|
|
575
|
-
/// - **OpenAI Responses API** (`/v1/responses`) — the request is
|
|
576
|
-
/// keyed under `input` (not `messages`) with item types like
|
|
577
|
-
/// `function_call_output` and `reasoning`; live zone is the
|
|
578
|
-
/// trailing function-call-output items since the last `message`
|
|
579
|
-
/// or `reasoning` item.
|
|
580
|
-
/// - **Google Gemini** (`/v1beta/.../:generateContent`) — request
|
|
581
|
-
/// is keyed under `contents` (not `messages`), with
|
|
582
|
-
/// `function_response` parts (not `tool_result`). Function
|
|
583
|
-
/// responses can be either string or structured object.
|
|
584
|
-
/// - **Bedrock InvokeModel** — the embedded payload follows the
|
|
585
|
-
/// model's native format (Anthropic, Llama, Cohere, …); route
|
|
586
|
-
/// to the matching dispatcher.
|
|
587
|
-
///
|
|
588
|
-
/// Phase C (`REALIGNMENT/05-phase-C-rust-proxy.md`) introduces the
|
|
589
|
-
/// per-provider dispatchers. Each will live as
|
|
590
|
-
/// `compress_<provider>_live_zone` and share the cache-safety
|
|
591
|
-
/// invariants and the per-content-type compressor backend
|
|
592
|
-
/// (SmartCrusher / LogCompressor / SearchCompressor /
|
|
593
|
-
/// DiffCompressor / Code) from this module. The
|
|
594
|
-
/// [`LiveZoneOutcome`], [`BlockAction`], and
|
|
595
|
-
/// [`CompressionManifest`] types are intentionally
|
|
596
|
-
/// provider-agnostic so the per-provider dispatchers can return
|
|
597
|
-
/// them unchanged.
|
|
598
|
-
///
|
|
599
|
-
/// # Arguments
|
|
600
|
-
///
|
|
601
|
-
/// - `body_raw`: the buffered request body as bytes. Must be valid
|
|
602
|
-
/// UTF-8 JSON; non-JSON returns [`LiveZoneError::BodyNotJson`].
|
|
603
|
-
/// - `frozen_message_count`: hot-zone floor. Indices `< floor` are
|
|
604
|
-
/// excluded from dispatch.
|
|
605
|
-
/// - `_auth_mode`: reserved for PR-F2; B3 ignores it.
|
|
606
|
-
/// - `model`: the upstream model name (e.g. `"claude-3-5-sonnet-20241022"`).
|
|
607
|
-
/// Routes the tokenizer registry to the right backend for the
|
|
608
|
-
/// per-block token-count check (PR-B4). Pass [`DEFAULT_MODEL`] when
|
|
609
|
-
/// the proxy could not extract `body["model"]`.
|
|
610
|
-
///
|
|
611
|
-
/// # Returns
|
|
612
|
-
///
|
|
613
|
-
/// - [`LiveZoneOutcome::NoChange`] when no block was rewritten
|
|
614
|
-
/// (either nothing was eligible, or every compressor declined /
|
|
615
|
-
/// failed / produced larger output).
|
|
616
|
-
/// - [`LiveZoneOutcome::Modified`] when at least one block was
|
|
617
|
-
/// rewritten — the proxy forwards the new body.
|
|
618
|
-
pub fn compress_anthropic_live_zone(
|
|
619
|
-
body_raw: &[u8],
|
|
620
|
-
frozen_message_count: usize,
|
|
621
|
-
auth_mode: AuthMode,
|
|
622
|
-
model: &str,
|
|
623
|
-
) -> Result<LiveZoneOutcome, LiveZoneError> {
|
|
624
|
-
compress_anthropic_live_zone_with_ccr(body_raw, frozen_message_count, auth_mode, model, None)
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/// Same as [`compress_anthropic_live_zone`] but with an optional
|
|
628
|
-
/// [`CcrStore`] for retrieval-marker injection (PR-B7).
|
|
629
|
-
///
|
|
630
|
-
/// When `ccr_store` is `Some(_)` and a compressor produces a strictly
|
|
631
|
-
/// smaller block, the dispatcher:
|
|
632
|
-
///
|
|
633
|
-
/// 1. Computes `hash = compute_key(original_bytes)` (BLAKE3 → 24 hex
|
|
634
|
-
/// chars).
|
|
635
|
-
/// 2. Stores the original block content in the backend under that hash.
|
|
636
|
-
/// 3. Appends the marker `<<ccr:HASH>>` to the compressed block content
|
|
637
|
-
/// (newline-separated) so the model can later call
|
|
638
|
-
/// `headroom_retrieve(hash="HASH")` to recover the original bytes.
|
|
639
|
-
///
|
|
640
|
-
/// When `ccr_store` is `None` (default for tests, default for the old
|
|
641
|
-
/// `compress_anthropic_live_zone` shim), the dispatcher behaves
|
|
642
|
-
/// identically to PR-B4 — no markers, no put.
|
|
643
|
-
pub fn compress_anthropic_live_zone_with_ccr(
|
|
644
|
-
body_raw: &[u8],
|
|
645
|
-
frozen_message_count: usize,
|
|
646
|
-
_auth_mode: AuthMode,
|
|
647
|
-
model: &str,
|
|
648
|
-
ccr_store: Option<&dyn CcrStore>,
|
|
649
|
-
) -> Result<LiveZoneOutcome, LiveZoneError> {
|
|
650
|
-
let parsed: Value = serde_json::from_slice(body_raw).map_err(LiveZoneError::BodyNotJson)?;
|
|
651
|
-
let messages = parsed
|
|
652
|
-
.get("messages")
|
|
653
|
-
.and_then(Value::as_array)
|
|
654
|
-
.ok_or(LiveZoneError::NoMessagesArray)?;
|
|
655
|
-
|
|
656
|
-
if messages.is_empty() {
|
|
657
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
658
|
-
manifest: CompressionManifest::empty(),
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
let messages_total = messages.len();
|
|
663
|
-
let messages_below_frozen_floor = frozen_message_count.min(messages_total);
|
|
664
|
-
|
|
665
|
-
// Latest user message index, restricted to the live zone (>= floor).
|
|
666
|
-
let latest_user_message_index = find_latest_user_message_index(messages, frozen_message_count);
|
|
667
|
-
|
|
668
|
-
let Some(target_idx) = latest_user_message_index else {
|
|
669
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
670
|
-
manifest: CompressionManifest {
|
|
671
|
-
messages_total,
|
|
672
|
-
messages_below_frozen_floor,
|
|
673
|
-
latest_user_message_index: None,
|
|
674
|
-
block_outcomes: Vec::new(),
|
|
675
|
-
},
|
|
676
|
-
});
|
|
677
|
-
};
|
|
678
|
-
|
|
679
|
-
// Resolve block ranges (byte offsets into `body_raw`) by walking
|
|
680
|
-
// the body via `RawValue` borrowed slices. The Vec<Replacement>
|
|
681
|
-
// produced here is the surgery plan; we do *not* mutate `body_raw`
|
|
682
|
-
// while computing it.
|
|
683
|
-
let plan = match plan_block_replacements(body_raw, target_idx) {
|
|
684
|
-
Ok(p) => p,
|
|
685
|
-
Err(_) => {
|
|
686
|
-
// Body shape doesn't match what we expect (e.g. content
|
|
687
|
-
// is not a string and not an array, or messages is shaped
|
|
688
|
-
// unexpectedly). Treat as no-change; the proxy forwards
|
|
689
|
-
// the original bytes verbatim.
|
|
690
|
-
let block_outcomes =
|
|
691
|
-
inspect_latest_user_blocks_value(&messages[target_idx], target_idx)
|
|
692
|
-
.unwrap_or_default();
|
|
693
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
694
|
-
manifest: CompressionManifest {
|
|
695
|
-
messages_total,
|
|
696
|
-
messages_below_frozen_floor,
|
|
697
|
-
latest_user_message_index: Some(target_idx),
|
|
698
|
-
block_outcomes,
|
|
699
|
-
},
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
};
|
|
703
|
-
|
|
704
|
-
let mut block_outcomes: Vec<BlockOutcome> = Vec::with_capacity(plan.len());
|
|
705
|
-
let mut replacements: Vec<Replacement> = Vec::new();
|
|
706
|
-
// One tokenizer per request — `get_tokenizer` is cheap (it
|
|
707
|
-
// returns a `Box<dyn Tokenizer>` over either a tiktoken-rs handle
|
|
708
|
-
// or an estimator) but counting once per block is a hot path.
|
|
709
|
-
// PR-B4 only invokes the tokenizer on blocks that actually
|
|
710
|
-
// produced compressed output; the byte-threshold gate filters
|
|
711
|
-
// sub-threshold content first.
|
|
712
|
-
let tokenizer = get_tokenizer(model);
|
|
713
|
-
|
|
714
|
-
for slot in plan {
|
|
715
|
-
let outcome = match slot.kind {
|
|
716
|
-
SlotKind::HotZone(block_type) => BlockOutcome {
|
|
717
|
-
message_index: target_idx,
|
|
718
|
-
block_index: Some(slot.block_index),
|
|
719
|
-
block_type,
|
|
720
|
-
action: BlockAction::Excluded {
|
|
721
|
-
reason: ExclusionReason::HotZoneBlockType,
|
|
722
|
-
},
|
|
723
|
-
},
|
|
724
|
-
SlotKind::Compressible {
|
|
725
|
-
block_type,
|
|
726
|
-
content_text,
|
|
727
|
-
content_byte_range,
|
|
728
|
-
} => {
|
|
729
|
-
let detected = detect_content_type(&content_text);
|
|
730
|
-
let outcome: BlockOutcome = compress_one_block(
|
|
731
|
-
&content_text,
|
|
732
|
-
detected.content_type,
|
|
733
|
-
content_byte_range,
|
|
734
|
-
target_idx,
|
|
735
|
-
Some(slot.block_index),
|
|
736
|
-
block_type,
|
|
737
|
-
tokenizer.as_ref(),
|
|
738
|
-
&mut replacements,
|
|
739
|
-
ccr_store,
|
|
740
|
-
);
|
|
741
|
-
outcome
|
|
742
|
-
}
|
|
743
|
-
SlotKind::StringContent {
|
|
744
|
-
content_text,
|
|
745
|
-
content_byte_range,
|
|
746
|
-
} => {
|
|
747
|
-
let detected = detect_content_type(&content_text);
|
|
748
|
-
compress_one_block(
|
|
749
|
-
&content_text,
|
|
750
|
-
detected.content_type,
|
|
751
|
-
content_byte_range,
|
|
752
|
-
target_idx,
|
|
753
|
-
None,
|
|
754
|
-
"string_content".to_string(),
|
|
755
|
-
tokenizer.as_ref(),
|
|
756
|
-
&mut replacements,
|
|
757
|
-
ccr_store,
|
|
758
|
-
)
|
|
759
|
-
}
|
|
760
|
-
};
|
|
761
|
-
block_outcomes.push(outcome);
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
let manifest = CompressionManifest {
|
|
765
|
-
messages_total,
|
|
766
|
-
messages_below_frozen_floor,
|
|
767
|
-
latest_user_message_index: Some(target_idx),
|
|
768
|
-
block_outcomes,
|
|
769
|
-
};
|
|
770
|
-
|
|
771
|
-
if !manifest.has_compressed_block() || replacements.is_empty() {
|
|
772
|
-
return Ok(LiveZoneOutcome::NoChange { manifest });
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
// Build the new body via byte-range surgery. Replacements are
|
|
776
|
-
// produced in ascending block order; sort defensively.
|
|
777
|
-
let new_bytes = apply_replacements(body_raw, &mut replacements);
|
|
778
|
-
|
|
779
|
-
// The output is always still valid JSON: every replacement is a
|
|
780
|
-
// JSON string slot replaced by another JSON string slot. We could
|
|
781
|
-
// round-trip-verify with `serde_json::from_slice` and bail out to
|
|
782
|
-
// NoChange on failure, but that doubles parse cost on the hot
|
|
783
|
-
// path. Rely on type discipline; the byte_fidelity test in
|
|
784
|
-
// `live_zone_dispatch.rs` pins correctness.
|
|
785
|
-
let new_body_str = match std::str::from_utf8(&new_bytes) {
|
|
786
|
-
Ok(s) => s,
|
|
787
|
-
Err(_) => {
|
|
788
|
-
// Should be impossible: input was valid JSON (UTF-8) and
|
|
789
|
-
// every replacement was a JSON-encoded string (also UTF-8).
|
|
790
|
-
// Fall back rather than risk shipping malformed bytes.
|
|
791
|
-
return Ok(LiveZoneOutcome::NoChange { manifest });
|
|
792
|
-
}
|
|
793
|
-
};
|
|
794
|
-
let raw = match RawValue::from_string(new_body_str.to_string()) {
|
|
795
|
-
Ok(r) => r,
|
|
796
|
-
Err(_) => {
|
|
797
|
-
// Same defensive bail-out; should not happen.
|
|
798
|
-
return Ok(LiveZoneOutcome::NoChange { manifest });
|
|
799
|
-
}
|
|
800
|
-
};
|
|
801
|
-
|
|
802
|
-
Ok(LiveZoneOutcome::Modified {
|
|
803
|
-
new_body: raw,
|
|
804
|
-
manifest,
|
|
805
|
-
})
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
// ─── Internal helpers ──────────────────────────────────────────────────
|
|
809
|
-
|
|
810
|
-
/// Per-block dispatch shared by the array-of-blocks slot and the
|
|
811
|
-
/// legacy string-content slot. Encapsulates the PR-B4 sequence:
|
|
812
|
-
///
|
|
813
|
-
/// 1. Per-content-type byte threshold — sub-threshold content is
|
|
814
|
-
/// tagged `BelowByteThreshold` and the dispatcher does not even
|
|
815
|
-
/// invoke a compressor.
|
|
816
|
-
/// 2. Type-aware dispatch (`dispatch_compressor`).
|
|
817
|
-
/// 3. Tokenizer-validated rejection — if `compressed_tokens >=
|
|
818
|
-
/// original_tokens` keep the original and tag `RejectedNotSmaller`
|
|
819
|
-
/// (note: tokens, not bytes, drive the gate).
|
|
820
|
-
/// 4. Otherwise record the replacement and tag `Compressed`.
|
|
821
|
-
#[allow(clippy::too_many_arguments)]
|
|
822
|
-
fn compress_one_block(
|
|
823
|
-
content_text: &str,
|
|
824
|
-
content_type: ContentType,
|
|
825
|
-
content_byte_range: (usize, usize),
|
|
826
|
-
message_index: usize,
|
|
827
|
-
block_index: Option<usize>,
|
|
828
|
-
block_type: String,
|
|
829
|
-
tokenizer: &dyn crate::tokenizer::Tokenizer,
|
|
830
|
-
replacements: &mut Vec<Replacement>,
|
|
831
|
-
ccr_store: Option<&dyn CcrStore>,
|
|
832
|
-
) -> BlockOutcome {
|
|
833
|
-
// 1. Byte-threshold gate. Empty content always falls through to
|
|
834
|
-
// `dispatch_compressor` (which short-circuits on empty), so
|
|
835
|
-
// only check when the slot has real bytes — this preserves
|
|
836
|
-
// the existing "tool_result with no inner content" pathway.
|
|
837
|
-
if !content_text.is_empty() && content_text.len() < threshold_for(content_type) {
|
|
838
|
-
return BlockOutcome {
|
|
839
|
-
message_index,
|
|
840
|
-
block_index,
|
|
841
|
-
block_type,
|
|
842
|
-
action: BlockAction::BelowByteThreshold {
|
|
843
|
-
content_type: content_type.as_str(),
|
|
844
|
-
byte_count: content_text.len(),
|
|
845
|
-
threshold_bytes: threshold_for(content_type),
|
|
846
|
-
},
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
match dispatch_compressor(content_text, content_type) {
|
|
851
|
-
DispatchResult::NoOp { content_type } => BlockOutcome {
|
|
852
|
-
message_index,
|
|
853
|
-
block_index,
|
|
854
|
-
block_type,
|
|
855
|
-
action: BlockAction::NoCompressionApplied {
|
|
856
|
-
content_type: content_type.to_string(),
|
|
857
|
-
},
|
|
858
|
-
},
|
|
859
|
-
DispatchResult::Compressed {
|
|
860
|
-
strategy,
|
|
861
|
-
compressed,
|
|
862
|
-
} => {
|
|
863
|
-
let original_bytes = content_text.len();
|
|
864
|
-
// PR-B7: when a CCR store is wired, persist the original
|
|
865
|
-
// block content keyed by `BLAKE3(original)[..24]` and append
|
|
866
|
-
// the `<<ccr:HASH>>` marker to the compressed string. The
|
|
867
|
-
// marker stays on a fresh trailing line so it is easy for
|
|
868
|
-
// the model to spot and so that the per-content-type
|
|
869
|
-
// compressors (which already produce trailing summary
|
|
870
|
-
// lines) keep their final newline before the marker.
|
|
871
|
-
//
|
|
872
|
-
// The token-validation gate (step 3) is computed against
|
|
873
|
-
// the marker-augmented string so the saved-token check
|
|
874
|
-
// stays honest — the marker costs ~6 tokens and we'd
|
|
875
|
-
// rather forward the original than ship a bigger payload
|
|
876
|
-
// for a 5-byte block.
|
|
877
|
-
let (compressed_for_replacement, ccr_hash_emitted) =
|
|
878
|
-
maybe_inject_ccr_marker(content_text, &compressed, ccr_store);
|
|
879
|
-
let compressed_bytes = compressed_for_replacement.len();
|
|
880
|
-
// 3. Tokenizer-validated rejection. Per PR-B4 spec we
|
|
881
|
-
// count both the original and compressed strings
|
|
882
|
-
// using the model's tokenizer; the compression is
|
|
883
|
-
// accepted only when it shrinks the token count.
|
|
884
|
-
// Bytes-shrinking-but-tokens-growing happens for
|
|
885
|
-
// pathological inputs (e.g. dense base64 → tokenizer
|
|
886
|
-
// fragments more aggressively after a transform).
|
|
887
|
-
let original_tokens = tokenizer.count_text(content_text);
|
|
888
|
-
let compressed_tokens = tokenizer.count_text(&compressed_for_replacement);
|
|
889
|
-
if compressed_tokens >= original_tokens {
|
|
890
|
-
BlockOutcome {
|
|
891
|
-
message_index,
|
|
892
|
-
block_index,
|
|
893
|
-
block_type,
|
|
894
|
-
action: BlockAction::RejectedNotSmaller {
|
|
895
|
-
strategy,
|
|
896
|
-
original_bytes,
|
|
897
|
-
compressed_bytes,
|
|
898
|
-
original_tokens,
|
|
899
|
-
compressed_tokens,
|
|
900
|
-
},
|
|
901
|
-
}
|
|
902
|
-
} else {
|
|
903
|
-
// Only persist to the CCR store once the rejection
|
|
904
|
-
// gate has admitted the compression — otherwise we
|
|
905
|
-
// populate the store with hashes whose markers
|
|
906
|
-
// never reach the wire (still correct, but wastes
|
|
907
|
-
// storage capacity).
|
|
908
|
-
if let (Some(store), Some(hash)) = (ccr_store, ccr_hash_emitted.as_deref()) {
|
|
909
|
-
store.put(hash, content_text);
|
|
910
|
-
}
|
|
911
|
-
let replacement_bytes = serde_json::to_vec(&compressed_for_replacement)
|
|
912
|
-
.expect("string is always JSON-encodable");
|
|
913
|
-
replacements.push(Replacement {
|
|
914
|
-
range: content_byte_range,
|
|
915
|
-
replacement: replacement_bytes,
|
|
916
|
-
});
|
|
917
|
-
BlockOutcome {
|
|
918
|
-
message_index,
|
|
919
|
-
block_index,
|
|
920
|
-
block_type,
|
|
921
|
-
action: BlockAction::Compressed {
|
|
922
|
-
strategy,
|
|
923
|
-
original_bytes,
|
|
924
|
-
compressed_bytes,
|
|
925
|
-
original_tokens,
|
|
926
|
-
compressed_tokens,
|
|
927
|
-
},
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
DispatchResult::Error { strategy, error } => BlockOutcome {
|
|
932
|
-
message_index,
|
|
933
|
-
block_index,
|
|
934
|
-
block_type,
|
|
935
|
-
action: BlockAction::CompressorError { strategy, error },
|
|
936
|
-
},
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
/// Walk `messages` from the back, returning the index of the latest
|
|
941
|
-
/// `role == "user"` message. Restricted to indices `>= floor`; if
|
|
942
|
-
/// the latest user message lies in the cache hot zone we return
|
|
943
|
-
/// `None` (it's out of bounds for live-zone work).
|
|
944
|
-
fn find_latest_user_message_index(messages: &[Value], floor: usize) -> Option<usize> {
|
|
945
|
-
let start = floor.min(messages.len());
|
|
946
|
-
for (offset, msg) in messages.iter().enumerate().rev() {
|
|
947
|
-
if offset < start {
|
|
948
|
-
return None;
|
|
949
|
-
}
|
|
950
|
-
if msg.get("role").and_then(Value::as_str) == Some("user") {
|
|
951
|
-
return Some(offset);
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
None
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
/// Body-shape view used to find byte ranges.
|
|
958
|
-
///
|
|
959
|
-
/// `&'a RawValue` borrows are pointer-equal to slices into the input
|
|
960
|
-
/// buffer; we use this to compute exact byte offsets via the
|
|
961
|
-
/// `bytes_offset_of` helper. The struct intentionally only captures
|
|
962
|
-
/// the path we need; everything else is left unparsed.
|
|
963
|
-
#[derive(Deserialize)]
|
|
964
|
-
struct BodyView<'a> {
|
|
965
|
-
#[serde(borrow)]
|
|
966
|
-
messages: Vec<&'a RawValue>,
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
#[derive(Deserialize)]
|
|
970
|
-
struct MessageView<'a> {
|
|
971
|
-
#[serde(borrow, default)]
|
|
972
|
-
content: Option<&'a RawValue>,
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
#[derive(Deserialize)]
|
|
976
|
-
struct BlockHeader<'a> {
|
|
977
|
-
#[serde(borrow, default)]
|
|
978
|
-
r#type: Option<&'a str>,
|
|
979
|
-
#[serde(borrow, default)]
|
|
980
|
-
content: Option<&'a RawValue>,
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
/// Per-block dispatch slot the planner emits.
|
|
984
|
-
struct PlanSlot {
|
|
985
|
-
block_index: usize,
|
|
986
|
-
kind: SlotKind,
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
enum SlotKind {
|
|
990
|
-
/// Content is a JSON string the dispatcher may compress in place.
|
|
991
|
-
Compressible {
|
|
992
|
-
block_type: String,
|
|
993
|
-
content_text: String,
|
|
994
|
-
content_byte_range: (usize, usize),
|
|
995
|
-
},
|
|
996
|
-
/// String-shaped message content (Anthropic legacy shape: the
|
|
997
|
-
/// whole message's `content` is a JSON string, no per-block
|
|
998
|
-
/// array).
|
|
999
|
-
StringContent {
|
|
1000
|
-
content_text: String,
|
|
1001
|
-
content_byte_range: (usize, usize),
|
|
1002
|
-
},
|
|
1003
|
-
/// Block type is on the cache-hot list — record but do not
|
|
1004
|
-
/// dispatch.
|
|
1005
|
-
HotZone(String),
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
/// Walk the buffered body, return one `PlanSlot` per block in the
|
|
1009
|
-
/// latest user message. Errors out on shapes the dispatcher does not
|
|
1010
|
-
/// support (e.g. structured-array `content` inside a tool_result —
|
|
1011
|
-
/// rare; we degrade to NoChange in that case).
|
|
1012
|
-
/// Whether a content block (no `type` key) carries a JSON-string `text`
|
|
1013
|
-
/// field — the Bedrock Converse text-block shape (`{"text": "..."}`).
|
|
1014
|
-
/// Used to route typeless Converse text through the Anthropic text path.
|
|
1015
|
-
/// Blocks whose `text` is absent or non-string (e.g. `{"image": ...}`,
|
|
1016
|
-
/// `{"toolUse": ...}`) return false and stay unrecognized → no-op.
|
|
1017
|
-
fn block_has_string_text_field(block_json: &str) -> bool {
|
|
1018
|
-
#[derive(Deserialize)]
|
|
1019
|
-
struct Probe<'a> {
|
|
1020
|
-
#[serde(borrow, default)]
|
|
1021
|
-
text: Option<&'a RawValue>,
|
|
1022
|
-
}
|
|
1023
|
-
serde_json::from_str::<Probe<'_>>(block_json)
|
|
1024
|
-
.ok()
|
|
1025
|
-
.and_then(|p| p.text)
|
|
1026
|
-
.is_some_and(|t| t.get().trim_start().starts_with('"'))
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
fn plan_block_replacements(
|
|
1030
|
-
body_raw: &[u8],
|
|
1031
|
-
target_msg_idx: usize,
|
|
1032
|
-
) -> Result<Vec<PlanSlot>, PlanError> {
|
|
1033
|
-
// `serde_json::from_slice` requires UTF-8; we re-validate here
|
|
1034
|
-
// explicitly so the pointer-arithmetic helper can take a `&str`
|
|
1035
|
-
// without unsafe.
|
|
1036
|
-
let body_str = std::str::from_utf8(body_raw).map_err(|_| PlanError::ParseFailed)?;
|
|
1037
|
-
let body: BodyView<'_> = serde_json::from_str(body_str).map_err(|_| PlanError::ParseFailed)?;
|
|
1038
|
-
let target_msg_raw = body
|
|
1039
|
-
.messages
|
|
1040
|
-
.get(target_msg_idx)
|
|
1041
|
-
.ok_or(PlanError::TargetOutOfBounds)?;
|
|
1042
|
-
|
|
1043
|
-
let msg_view: MessageView<'_> =
|
|
1044
|
-
serde_json::from_str(target_msg_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
1045
|
-
|
|
1046
|
-
let Some(content_raw) = msg_view.content else {
|
|
1047
|
-
return Ok(Vec::new());
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
// Compute the byte offset of the message's `content` value into
|
|
1051
|
-
// `body_raw`. The target_msg_raw points into body_raw; content_raw
|
|
1052
|
-
// points into target_msg_raw's bytes (which are the same backing
|
|
1053
|
-
// memory).
|
|
1054
|
-
let content_offset_in_msg =
|
|
1055
|
-
bytes_offset_of(target_msg_raw.get(), content_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
1056
|
-
let msg_offset_in_body =
|
|
1057
|
-
bytes_offset_of(body_str, target_msg_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
1058
|
-
let content_offset_in_body = msg_offset_in_body + content_offset_in_msg;
|
|
1059
|
-
|
|
1060
|
-
let content_str = content_raw.get();
|
|
1061
|
-
|
|
1062
|
-
// Case 1: content is a JSON string (Anthropic legacy shape for
|
|
1063
|
-
// user messages).
|
|
1064
|
-
if content_str.starts_with('"') {
|
|
1065
|
-
let unescaped: String =
|
|
1066
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
1067
|
-
return Ok(vec![PlanSlot {
|
|
1068
|
-
block_index: 0,
|
|
1069
|
-
kind: SlotKind::StringContent {
|
|
1070
|
-
content_text: unescaped,
|
|
1071
|
-
content_byte_range: (
|
|
1072
|
-
content_offset_in_body,
|
|
1073
|
-
content_offset_in_body + content_str.len(),
|
|
1074
|
-
),
|
|
1075
|
-
},
|
|
1076
|
-
}]);
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
// Case 2: content is an array of blocks. Borrow each block as a
|
|
1080
|
-
// &RawValue so we can compute its byte range too.
|
|
1081
|
-
let blocks: Vec<&RawValue> =
|
|
1082
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
1083
|
-
|
|
1084
|
-
let mut slots = Vec::with_capacity(blocks.len());
|
|
1085
|
-
for (block_idx, block_raw) in blocks.iter().enumerate() {
|
|
1086
|
-
let block_offset_in_content =
|
|
1087
|
-
bytes_offset_of(content_str, block_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
1088
|
-
let block_offset_in_body = content_offset_in_body + block_offset_in_content;
|
|
1089
|
-
|
|
1090
|
-
let header: BlockHeader<'_> =
|
|
1091
|
-
serde_json::from_str(block_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
1092
|
-
// Bedrock Converse content blocks carry no `type` discriminator —
|
|
1093
|
-
// the variant is the key itself (`{"text": ...}`, `{"image": ...}`,
|
|
1094
|
-
// `{"toolUse": ...}`). A typeless block whose `text` field is a
|
|
1095
|
-
// JSON string is Converse text; route it through the same surgical
|
|
1096
|
-
// path as an Anthropic `{"type":"text","text":...}` block so
|
|
1097
|
-
// Converse user-message text compresses too. Anthropic blocks
|
|
1098
|
-
// always carry `type`, so this never alters the Anthropic path.
|
|
1099
|
-
let block_type = match header.r#type {
|
|
1100
|
-
Some(t) => t.to_string(),
|
|
1101
|
-
None if block_has_string_text_field(block_raw.get()) => "text".to_string(),
|
|
1102
|
-
None => "unknown".to_string(),
|
|
1103
|
-
};
|
|
1104
|
-
|
|
1105
|
-
if HOT_ZONE_BLOCK_TYPES.iter().any(|t| *t == block_type) {
|
|
1106
|
-
slots.push(PlanSlot {
|
|
1107
|
-
block_index: block_idx,
|
|
1108
|
-
kind: SlotKind::HotZone(block_type),
|
|
1109
|
-
});
|
|
1110
|
-
continue;
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
// Find the inner `content` field's byte range. For tool_result
|
|
1114
|
-
// blocks this is the field we'd compress. For text blocks
|
|
1115
|
-
// it's a `text` field — we read that instead.
|
|
1116
|
-
let (inner_field_str, inner_field_offset_in_block) = match block_type.as_str() {
|
|
1117
|
-
"tool_result" => {
|
|
1118
|
-
let Some(field_raw) = header.content else {
|
|
1119
|
-
// tool_result with no content — skip dispatch.
|
|
1120
|
-
slots.push(PlanSlot {
|
|
1121
|
-
block_index: block_idx,
|
|
1122
|
-
kind: SlotKind::Compressible {
|
|
1123
|
-
block_type,
|
|
1124
|
-
content_text: String::new(),
|
|
1125
|
-
content_byte_range: (block_offset_in_body, block_offset_in_body),
|
|
1126
|
-
},
|
|
1127
|
-
});
|
|
1128
|
-
continue;
|
|
1129
|
-
};
|
|
1130
|
-
let off = bytes_offset_of(block_raw.get(), field_raw.get())
|
|
1131
|
-
.ok_or(PlanError::OffsetMissing)?;
|
|
1132
|
-
(field_raw.get(), off)
|
|
1133
|
-
}
|
|
1134
|
-
"text" => {
|
|
1135
|
-
#[derive(Deserialize)]
|
|
1136
|
-
struct TextHeader<'a> {
|
|
1137
|
-
#[serde(borrow, default)]
|
|
1138
|
-
text: Option<&'a RawValue>,
|
|
1139
|
-
}
|
|
1140
|
-
let h: TextHeader<'_> =
|
|
1141
|
-
serde_json::from_str(block_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
1142
|
-
let Some(text_raw) = h.text else {
|
|
1143
|
-
slots.push(PlanSlot {
|
|
1144
|
-
block_index: block_idx,
|
|
1145
|
-
kind: SlotKind::Compressible {
|
|
1146
|
-
block_type,
|
|
1147
|
-
content_text: String::new(),
|
|
1148
|
-
content_byte_range: (block_offset_in_body, block_offset_in_body),
|
|
1149
|
-
},
|
|
1150
|
-
});
|
|
1151
|
-
continue;
|
|
1152
|
-
};
|
|
1153
|
-
let off = bytes_offset_of(block_raw.get(), text_raw.get())
|
|
1154
|
-
.ok_or(PlanError::OffsetMissing)?;
|
|
1155
|
-
(text_raw.get(), off)
|
|
1156
|
-
}
|
|
1157
|
-
_ => {
|
|
1158
|
-
// image, document, etc. — record as compressible
|
|
1159
|
-
// block-type but with empty content so no compressor
|
|
1160
|
-
// runs.
|
|
1161
|
-
slots.push(PlanSlot {
|
|
1162
|
-
block_index: block_idx,
|
|
1163
|
-
kind: SlotKind::Compressible {
|
|
1164
|
-
block_type,
|
|
1165
|
-
content_text: String::new(),
|
|
1166
|
-
content_byte_range: (block_offset_in_body, block_offset_in_body),
|
|
1167
|
-
},
|
|
1168
|
-
});
|
|
1169
|
-
continue;
|
|
1170
|
-
}
|
|
1171
|
-
};
|
|
1172
|
-
|
|
1173
|
-
// The compressors expect a plain string, not a JSON-quoted
|
|
1174
|
-
// string. `tool_result.content` and `text.text` are
|
|
1175
|
-
// either a JSON string or a structured array; we only
|
|
1176
|
-
// compress the string shape (B3). Structured-array shape
|
|
1177
|
-
// falls through to no-op.
|
|
1178
|
-
if !inner_field_str.starts_with('"') {
|
|
1179
|
-
slots.push(PlanSlot {
|
|
1180
|
-
block_index: block_idx,
|
|
1181
|
-
kind: SlotKind::Compressible {
|
|
1182
|
-
block_type,
|
|
1183
|
-
content_text: String::new(),
|
|
1184
|
-
content_byte_range: (block_offset_in_body, block_offset_in_body),
|
|
1185
|
-
},
|
|
1186
|
-
});
|
|
1187
|
-
continue;
|
|
1188
|
-
}
|
|
1189
|
-
let unescaped: String =
|
|
1190
|
-
serde_json::from_str(inner_field_str).map_err(|_| PlanError::ParseFailed)?;
|
|
1191
|
-
|
|
1192
|
-
let inner_field_start_in_body = block_offset_in_body + inner_field_offset_in_block;
|
|
1193
|
-
let inner_field_end_in_body = inner_field_start_in_body + inner_field_str.len();
|
|
1194
|
-
|
|
1195
|
-
slots.push(PlanSlot {
|
|
1196
|
-
block_index: block_idx,
|
|
1197
|
-
kind: SlotKind::Compressible {
|
|
1198
|
-
block_type,
|
|
1199
|
-
content_text: unescaped,
|
|
1200
|
-
content_byte_range: (inner_field_start_in_body, inner_field_end_in_body),
|
|
1201
|
-
},
|
|
1202
|
-
});
|
|
1203
|
-
}
|
|
1204
|
-
|
|
1205
|
-
Ok(slots)
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
#[derive(Debug)]
|
|
1209
|
-
enum PlanError {
|
|
1210
|
-
/// JSON parse failure on a body-shape view we expected to succeed.
|
|
1211
|
-
ParseFailed,
|
|
1212
|
-
/// Pointer-arithmetic could not locate a sub-slice's offset.
|
|
1213
|
-
/// Should not happen for valid JSON; surfacing rather than
|
|
1214
|
-
/// silently degrading.
|
|
1215
|
-
OffsetMissing,
|
|
1216
|
-
/// Latest-user-message index points past the end of `messages`.
|
|
1217
|
-
/// The caller already validated this — surfacing for safety.
|
|
1218
|
-
TargetOutOfBounds,
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
/// Compute the byte offset of `child` within `parent` when both are
|
|
1222
|
-
/// `&str` views into the same backing memory. Returns `None` when
|
|
1223
|
-
/// `child` does not lie strictly inside `parent`.
|
|
1224
|
-
///
|
|
1225
|
-
/// We rely on this trick because `serde_json` does not expose the
|
|
1226
|
-
/// byte offset of a `&RawValue`; the `RawValue::get()` slice points
|
|
1227
|
-
/// into the input buffer when `from_slice` / `from_str` was used,
|
|
1228
|
-
/// so pointer arithmetic recovers it.
|
|
1229
|
-
fn bytes_offset_of(parent: &str, child: &str) -> Option<usize> {
|
|
1230
|
-
let parent_start = parent.as_ptr() as usize;
|
|
1231
|
-
let parent_end = parent_start + parent.len();
|
|
1232
|
-
let child_start = child.as_ptr() as usize;
|
|
1233
|
-
if child_start < parent_start || child_start + child.len() > parent_end {
|
|
1234
|
-
return None;
|
|
1235
|
-
}
|
|
1236
|
-
Some(child_start - parent_start)
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
/// One byte-range replacement to apply. Sorted in ascending `range.0`
|
|
1240
|
-
/// before splicing.
|
|
1241
|
-
struct Replacement {
|
|
1242
|
-
range: (usize, usize),
|
|
1243
|
-
replacement: Vec<u8>,
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
/// Apply all `replacements` to `original`, returning the new buffer.
|
|
1247
|
-
/// `replacements` are sorted in-place by ascending start offset; the
|
|
1248
|
-
/// caller may inspect them post-call (they remain valid).
|
|
1249
|
-
fn apply_replacements(original: &[u8], replacements: &mut [Replacement]) -> Vec<u8> {
|
|
1250
|
-
replacements.sort_by_key(|r| r.range.0);
|
|
1251
|
-
|
|
1252
|
-
// Pre-size: original_len - sum(removed) + sum(replacement_len).
|
|
1253
|
-
let removed: usize = replacements.iter().map(|r| r.range.1 - r.range.0).sum();
|
|
1254
|
-
let added: usize = replacements.iter().map(|r| r.replacement.len()).sum();
|
|
1255
|
-
let mut out = Vec::with_capacity(original.len().saturating_sub(removed) + added);
|
|
1256
|
-
|
|
1257
|
-
let mut cursor = 0usize;
|
|
1258
|
-
for r in replacements.iter() {
|
|
1259
|
-
out.extend_from_slice(&original[cursor..r.range.0]);
|
|
1260
|
-
out.extend_from_slice(&r.replacement);
|
|
1261
|
-
cursor = r.range.1;
|
|
1262
|
-
}
|
|
1263
|
-
out.extend_from_slice(&original[cursor..]);
|
|
1264
|
-
out
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
/// PR-B7: append a `<<ccr:HASH>>` retrieval marker to the compressed
|
|
1268
|
-
/// block content when a CCR store is wired. Returns the
|
|
1269
|
-
/// (possibly-augmented) compressed string and the hash that was
|
|
1270
|
-
/// emitted (so the caller can decide whether to put the original into
|
|
1271
|
-
/// the store after the rejection gate). When `ccr_store` is `None`,
|
|
1272
|
-
/// returns the input compressed string unchanged with `None`.
|
|
1273
|
-
///
|
|
1274
|
-
/// The marker is appended on its own line — `\n<<ccr:HASH>>` — so:
|
|
1275
|
-
///
|
|
1276
|
-
/// 1. The marker is unambiguously after the compressor's last byte,
|
|
1277
|
-
/// even if that byte was a newline already (we only add one).
|
|
1278
|
-
/// 2. Markers are easy to detect in human-readable diffs / logs.
|
|
1279
|
-
/// 3. The Python `inject_ccr_retrieve_tool` regex in
|
|
1280
|
-
/// `headroom/ccr/tool_injection.py` keeps working — it matches
|
|
1281
|
-
/// `[a-f0-9]{24}` anywhere in the text.
|
|
1282
|
-
fn maybe_inject_ccr_marker(
|
|
1283
|
-
original: &str,
|
|
1284
|
-
compressed: &str,
|
|
1285
|
-
ccr_store: Option<&dyn CcrStore>,
|
|
1286
|
-
) -> (String, Option<String>) {
|
|
1287
|
-
if ccr_store.is_none() {
|
|
1288
|
-
return (compressed.to_string(), None);
|
|
1289
|
-
}
|
|
1290
|
-
let hash = compute_key(original.as_bytes());
|
|
1291
|
-
let marker = marker_for(&hash);
|
|
1292
|
-
let augmented = if compressed.ends_with('\n') {
|
|
1293
|
-
format!("{compressed}{marker}")
|
|
1294
|
-
} else {
|
|
1295
|
-
format!("{compressed}\n{marker}")
|
|
1296
|
-
};
|
|
1297
|
-
(augmented, Some(hash))
|
|
1298
|
-
}
|
|
1299
|
-
|
|
1300
|
-
/// Per-block dispatch result — whether any compressor ran and what
|
|
1301
|
-
/// it produced.
|
|
1302
|
-
enum DispatchResult {
|
|
1303
|
-
/// No compressor was applicable for this content type.
|
|
1304
|
-
NoOp { content_type: &'static str },
|
|
1305
|
-
/// A compressor ran and produced a candidate replacement string.
|
|
1306
|
-
Compressed {
|
|
1307
|
-
strategy: &'static str,
|
|
1308
|
-
compressed: String,
|
|
1309
|
-
},
|
|
1310
|
-
/// A compressor ran and failed loudly. The error string is
|
|
1311
|
-
/// surfaced via the manifest; the proxy logs it.
|
|
1312
|
-
#[allow(dead_code)]
|
|
1313
|
-
Error {
|
|
1314
|
-
strategy: &'static str,
|
|
1315
|
-
error: String,
|
|
1316
|
-
},
|
|
1317
|
-
}
|
|
1318
|
-
|
|
1319
|
-
/// Map `(text, content_type)` to the compressor result.
|
|
1320
|
-
///
|
|
1321
|
-
/// Per spec PR-B3:
|
|
1322
|
-
///
|
|
1323
|
-
/// - `JsonArray` (with `is_dict_array=true`) → SmartCrusher
|
|
1324
|
-
/// - `BuildOutput` → LogCompressor
|
|
1325
|
-
/// - `SearchResults` → SearchCompressor
|
|
1326
|
-
/// - `GitDiff` → DiffCompressor
|
|
1327
|
-
/// - `SourceCode` → no-op (Rust port pending; see TODO below)
|
|
1328
|
-
/// - `PlainText` → no-op (PR-B4 wires Kompress)
|
|
1329
|
-
/// - `Html` → no-op (no compressor)
|
|
1330
|
-
fn dispatch_compressor(text: &str, content_type: ContentType) -> DispatchResult {
|
|
1331
|
-
if text.is_empty() {
|
|
1332
|
-
return DispatchResult::NoOp {
|
|
1333
|
-
content_type: content_type.as_str(),
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
|
-
|
|
1337
|
-
match content_type {
|
|
1338
|
-
ContentType::JsonArray => {
|
|
1339
|
-
// The detector classifies arrays-of-scalars as JsonArray
|
|
1340
|
-
// too (confidence 0.8). SmartCrusher's `crush` is safe to
|
|
1341
|
-
// call on those — it parses, finds no compressible
|
|
1342
|
-
// arrays, and returns the input.
|
|
1343
|
-
let result = smart_crusher().crush(text, EMPTY_QUERY, DEFAULT_BIAS);
|
|
1344
|
-
if !result.was_modified {
|
|
1345
|
-
return DispatchResult::NoOp {
|
|
1346
|
-
content_type: content_type.as_str(),
|
|
1347
|
-
};
|
|
1348
|
-
}
|
|
1349
|
-
DispatchResult::Compressed {
|
|
1350
|
-
strategy: STRATEGY_SMART_CRUSHER,
|
|
1351
|
-
compressed: result.compressed,
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
ContentType::BuildOutput => {
|
|
1355
|
-
let (result, _stats) = log_compressor().compress(text, DEFAULT_BIAS);
|
|
1356
|
-
if result.compressed == result.original {
|
|
1357
|
-
return DispatchResult::NoOp {
|
|
1358
|
-
content_type: content_type.as_str(),
|
|
1359
|
-
};
|
|
1360
|
-
}
|
|
1361
|
-
DispatchResult::Compressed {
|
|
1362
|
-
strategy: STRATEGY_LOG_COMPRESSOR,
|
|
1363
|
-
compressed: result.compressed,
|
|
1364
|
-
}
|
|
1365
|
-
}
|
|
1366
|
-
ContentType::SearchResults => {
|
|
1367
|
-
let (result, _stats) = search_compressor().compress(text, EMPTY_QUERY, DEFAULT_BIAS);
|
|
1368
|
-
if result.compressed == result.original {
|
|
1369
|
-
return DispatchResult::NoOp {
|
|
1370
|
-
content_type: content_type.as_str(),
|
|
1371
|
-
};
|
|
1372
|
-
}
|
|
1373
|
-
DispatchResult::Compressed {
|
|
1374
|
-
strategy: STRATEGY_SEARCH_COMPRESSOR,
|
|
1375
|
-
compressed: result.compressed,
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
ContentType::GitDiff => {
|
|
1379
|
-
let result = diff_compressor().compress(text, EMPTY_QUERY);
|
|
1380
|
-
if result.compressed == text {
|
|
1381
|
-
return DispatchResult::NoOp {
|
|
1382
|
-
content_type: content_type.as_str(),
|
|
1383
|
-
};
|
|
1384
|
-
}
|
|
1385
|
-
DispatchResult::Compressed {
|
|
1386
|
-
strategy: STRATEGY_DIFF_COMPRESSOR,
|
|
1387
|
-
compressed: result.compressed,
|
|
1388
|
-
}
|
|
1389
|
-
}
|
|
1390
|
-
// TODO(PR-B4 / Rust code-compressor port): Python has a
|
|
1391
|
-
// CodeAwareCompressor; the Rust port is not yet shipped. Once
|
|
1392
|
-
// that crate lands, `ContentType::SourceCode` routes here
|
|
1393
|
-
// exactly as the others above.
|
|
1394
|
-
ContentType::SourceCode => DispatchResult::NoOp {
|
|
1395
|
-
content_type: content_type.as_str(),
|
|
1396
|
-
},
|
|
1397
|
-
// TODO(PR-B4): wire Kompress (lossless prose compressor) for
|
|
1398
|
-
// PlainText. For now, leave untouched.
|
|
1399
|
-
ContentType::PlainText => DispatchResult::NoOp {
|
|
1400
|
-
content_type: content_type.as_str(),
|
|
1401
|
-
},
|
|
1402
|
-
// No HTML compressor on the Rust side; pages are handled by
|
|
1403
|
-
// upstream extractors, not the proxy.
|
|
1404
|
-
ContentType::Html => DispatchResult::NoOp {
|
|
1405
|
-
content_type: content_type.as_str(),
|
|
1406
|
-
},
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
|
|
1410
|
-
/// Fallback when byte-range planning fails: still record per-block
|
|
1411
|
-
/// outcomes so observability covers the request. Mirrors PR-B2's
|
|
1412
|
-
/// observation-only path.
|
|
1413
|
-
fn inspect_latest_user_blocks_value(
|
|
1414
|
-
message: &Value,
|
|
1415
|
-
message_index: usize,
|
|
1416
|
-
) -> Option<Vec<BlockOutcome>> {
|
|
1417
|
-
let content = message.get("content")?;
|
|
1418
|
-
|
|
1419
|
-
if content.as_str().is_some() {
|
|
1420
|
-
return Some(vec![BlockOutcome {
|
|
1421
|
-
message_index,
|
|
1422
|
-
block_index: None,
|
|
1423
|
-
block_type: "string_content".to_string(),
|
|
1424
|
-
action: BlockAction::NoCompressionApplied {
|
|
1425
|
-
content_type: "text".to_string(),
|
|
1426
|
-
},
|
|
1427
|
-
}]);
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1430
|
-
let blocks = content.as_array()?;
|
|
1431
|
-
let mut outcomes = Vec::with_capacity(blocks.len());
|
|
1432
|
-
for (idx, block) in blocks.iter().enumerate() {
|
|
1433
|
-
let block_type = block
|
|
1434
|
-
.get("type")
|
|
1435
|
-
.and_then(Value::as_str)
|
|
1436
|
-
.unwrap_or("unknown")
|
|
1437
|
-
.to_string();
|
|
1438
|
-
let action = if HOT_ZONE_BLOCK_TYPES.iter().any(|t| *t == block_type) {
|
|
1439
|
-
BlockAction::Excluded {
|
|
1440
|
-
reason: ExclusionReason::HotZoneBlockType,
|
|
1441
|
-
}
|
|
1442
|
-
} else {
|
|
1443
|
-
BlockAction::NoCompressionApplied {
|
|
1444
|
-
content_type: "unknown".to_string(),
|
|
1445
|
-
}
|
|
1446
|
-
};
|
|
1447
|
-
outcomes.push(BlockOutcome {
|
|
1448
|
-
message_index,
|
|
1449
|
-
block_index: Some(idx),
|
|
1450
|
-
block_type,
|
|
1451
|
-
action,
|
|
1452
|
-
});
|
|
1453
|
-
}
|
|
1454
|
-
Some(outcomes)
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
#[cfg(test)]
|
|
1458
|
-
mod tests {
|
|
1459
|
-
use super::*;
|
|
1460
|
-
use serde_json::json;
|
|
1461
|
-
|
|
1462
|
-
fn body(value: Value) -> Vec<u8> {
|
|
1463
|
-
serde_json::to_vec(&value).unwrap()
|
|
1464
|
-
}
|
|
1465
|
-
|
|
1466
|
-
fn outcome_block_actions(o: &LiveZoneOutcome) -> Vec<&BlockAction> {
|
|
1467
|
-
let manifest = match o {
|
|
1468
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1469
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1470
|
-
};
|
|
1471
|
-
manifest.block_outcomes.iter().map(|b| &b.action).collect()
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
#[test]
|
|
1475
|
-
fn empty_messages_yields_no_change() {
|
|
1476
|
-
let b = body(json!({"model": "claude", "messages": []}));
|
|
1477
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1478
|
-
match out {
|
|
1479
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
1480
|
-
assert_eq!(manifest.messages_total, 0);
|
|
1481
|
-
assert_eq!(manifest.latest_user_message_index, None);
|
|
1482
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
1483
|
-
}
|
|
1484
|
-
_ => panic!("expected NoChange"),
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
|
|
1488
|
-
#[test]
|
|
1489
|
-
fn no_messages_field_errors() {
|
|
1490
|
-
let b = body(json!({"model": "claude"}));
|
|
1491
|
-
let err = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap_err();
|
|
1492
|
-
assert!(matches!(err, LiveZoneError::NoMessagesArray));
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
|
-
#[test]
|
|
1496
|
-
fn invalid_json_errors() {
|
|
1497
|
-
let err = compress_anthropic_live_zone(b"not json", 0, AuthMode::Payg, DEFAULT_MODEL)
|
|
1498
|
-
.unwrap_err();
|
|
1499
|
-
assert!(matches!(err, LiveZoneError::BodyNotJson(_)));
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
|
-
#[test]
|
|
1503
|
-
fn dispatches_only_to_latest_user_message() {
|
|
1504
|
-
// Two user messages; the dispatcher must pick the second (index 2).
|
|
1505
|
-
let b = body(json!({
|
|
1506
|
-
"messages": [
|
|
1507
|
-
{"role": "user", "content": "first user"},
|
|
1508
|
-
{"role": "assistant", "content": "first asst"},
|
|
1509
|
-
{"role": "user", "content": [
|
|
1510
|
-
{"type": "tool_result", "tool_use_id": "t1", "content": "result"},
|
|
1511
|
-
{"type": "text", "text": "summarize"}
|
|
1512
|
-
]},
|
|
1513
|
-
]
|
|
1514
|
-
}));
|
|
1515
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1516
|
-
let manifest = match &out {
|
|
1517
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1518
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1519
|
-
};
|
|
1520
|
-
assert_eq!(manifest.latest_user_message_index, Some(2));
|
|
1521
|
-
let block_msg_indices: Vec<usize> = manifest
|
|
1522
|
-
.block_outcomes
|
|
1523
|
-
.iter()
|
|
1524
|
-
.map(|b| b.message_index)
|
|
1525
|
-
.collect();
|
|
1526
|
-
assert!(
|
|
1527
|
-
block_msg_indices.iter().all(|i| *i == 2),
|
|
1528
|
-
"all block outcomes must reference the latest user message; got {block_msg_indices:?}"
|
|
1529
|
-
);
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
#[test]
|
|
1533
|
-
fn respects_frozen_message_count() {
|
|
1534
|
-
// Latest user message is at index 1; floor is 2 → live zone is empty.
|
|
1535
|
-
let b = body(json!({
|
|
1536
|
-
"messages": [
|
|
1537
|
-
{"role": "user", "content": "first"},
|
|
1538
|
-
{"role": "user", "content": [{"type": "text", "text": "second"}]},
|
|
1539
|
-
]
|
|
1540
|
-
}));
|
|
1541
|
-
let out = compress_anthropic_live_zone(&b, 2, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1542
|
-
let manifest = match &out {
|
|
1543
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1544
|
-
_ => panic!("expected NoChange"),
|
|
1545
|
-
};
|
|
1546
|
-
assert_eq!(manifest.latest_user_message_index, None);
|
|
1547
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
1548
|
-
assert_eq!(manifest.messages_below_frozen_floor, 2);
|
|
1549
|
-
}
|
|
1550
|
-
|
|
1551
|
-
#[test]
|
|
1552
|
-
fn excludes_hot_zone_block_types() {
|
|
1553
|
-
let b = body(json!({
|
|
1554
|
-
"messages": [{
|
|
1555
|
-
"role": "user",
|
|
1556
|
-
"content": [
|
|
1557
|
-
{"type": "tool_result", "tool_use_id": "t", "content": "x"},
|
|
1558
|
-
{"type": "thinking", "thinking": "...", "signature": "sig"},
|
|
1559
|
-
{"type": "text", "text": "ok"},
|
|
1560
|
-
]
|
|
1561
|
-
}]
|
|
1562
|
-
}));
|
|
1563
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1564
|
-
let actions = outcome_block_actions(&out);
|
|
1565
|
-
assert_eq!(actions.len(), 3);
|
|
1566
|
-
// tool_result with tiny content → BelowByteThreshold.
|
|
1567
|
-
assert!(matches!(actions[0], BlockAction::BelowByteThreshold { .. }));
|
|
1568
|
-
assert!(matches!(
|
|
1569
|
-
actions[1],
|
|
1570
|
-
BlockAction::Excluded {
|
|
1571
|
-
reason: ExclusionReason::HotZoneBlockType
|
|
1572
|
-
}
|
|
1573
|
-
));
|
|
1574
|
-
// text block with "ok" → BelowByteThreshold.
|
|
1575
|
-
assert!(matches!(actions[2], BlockAction::BelowByteThreshold { .. }));
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
#[test]
|
|
1579
|
-
fn string_content_message_records_synthetic_block() {
|
|
1580
|
-
let b = body(json!({
|
|
1581
|
-
"messages": [{"role": "user", "content": "just a string"}]
|
|
1582
|
-
}));
|
|
1583
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1584
|
-
let manifest = match &out {
|
|
1585
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1586
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1587
|
-
};
|
|
1588
|
-
assert_eq!(manifest.block_outcomes.len(), 1);
|
|
1589
|
-
assert_eq!(manifest.block_outcomes[0].block_type, "string_content");
|
|
1590
|
-
// 13 bytes of plain text is well below the plain-text threshold.
|
|
1591
|
-
assert!(matches!(
|
|
1592
|
-
manifest.block_outcomes[0].action,
|
|
1593
|
-
BlockAction::BelowByteThreshold { .. }
|
|
1594
|
-
));
|
|
1595
|
-
}
|
|
1596
|
-
|
|
1597
|
-
#[test]
|
|
1598
|
-
fn no_user_message_in_live_zone_returns_no_blocks() {
|
|
1599
|
-
let b = body(json!({
|
|
1600
|
-
"messages": [{"role": "assistant", "content": "hi"}]
|
|
1601
|
-
}));
|
|
1602
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1603
|
-
let manifest = match &out {
|
|
1604
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1605
|
-
_ => panic!("expected NoChange"),
|
|
1606
|
-
};
|
|
1607
|
-
assert_eq!(manifest.latest_user_message_index, None);
|
|
1608
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
#[test]
|
|
1612
|
-
fn auth_mode_does_not_affect_b3_outcome_for_short_input() {
|
|
1613
|
-
// Trivial input → every mode behaves identically.
|
|
1614
|
-
let b = body(json!({
|
|
1615
|
-
"messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}]
|
|
1616
|
-
}));
|
|
1617
|
-
let payg = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1618
|
-
let oauth = compress_anthropic_live_zone(&b, 0, AuthMode::OAuth, DEFAULT_MODEL).unwrap();
|
|
1619
|
-
let sub =
|
|
1620
|
-
compress_anthropic_live_zone(&b, 0, AuthMode::Subscription, DEFAULT_MODEL).unwrap();
|
|
1621
|
-
for o in [&payg, &oauth, &sub] {
|
|
1622
|
-
assert!(matches!(o, LiveZoneOutcome::NoChange { .. }));
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
|
-
#[test]
|
|
1627
|
-
fn no_change_when_input_already_minimal_returns_original_semantics() {
|
|
1628
|
-
// tiny tool_result → detected as plain text, no-op
|
|
1629
|
-
// dispatch → NoChange.
|
|
1630
|
-
let b = body(json!({
|
|
1631
|
-
"messages": [{
|
|
1632
|
-
"role": "user",
|
|
1633
|
-
"content": [
|
|
1634
|
-
{"type": "tool_result", "tool_use_id": "t", "content": "x"},
|
|
1635
|
-
]
|
|
1636
|
-
}]
|
|
1637
|
-
}));
|
|
1638
|
-
let out = compress_anthropic_live_zone(&b, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1639
|
-
assert!(matches!(out, LiveZoneOutcome::NoChange { .. }));
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
#[test]
|
|
1643
|
-
fn block_has_string_text_field_detects_converse_text_only() {
|
|
1644
|
-
// Converse text block: typeless, string `text` → recognized.
|
|
1645
|
-
assert!(block_has_string_text_field(r#"{"text":"hello"}"#));
|
|
1646
|
-
// Non-text Converse blocks must NOT be mistaken for text.
|
|
1647
|
-
assert!(!block_has_string_text_field(
|
|
1648
|
-
r#"{"image":{"format":"png"}}"#
|
|
1649
|
-
));
|
|
1650
|
-
assert!(!block_has_string_text_field(r#"{"toolUse":{"name":"x"}}"#));
|
|
1651
|
-
// `text` present but not a JSON string → not Converse text.
|
|
1652
|
-
assert!(!block_has_string_text_field(r#"{"text":["a"]}"#));
|
|
1653
|
-
assert!(!block_has_string_text_field(r#"{"text":{"v":1}}"#));
|
|
1654
|
-
}
|
|
1655
|
-
|
|
1656
|
-
#[test]
|
|
1657
|
-
fn converse_typeless_text_block_routes_like_anthropic_text() {
|
|
1658
|
-
// Bedrock Converse content blocks omit the `type` discriminator —
|
|
1659
|
-
// `{"text": "..."}` instead of `{"type":"text","text":"..."}`. The
|
|
1660
|
-
// dispatcher must treat the two identically so Converse user-message
|
|
1661
|
-
// text compresses like Anthropic text.
|
|
1662
|
-
let payload = "{\"k\": \"v\", \"n\": 1}\n".repeat(200);
|
|
1663
|
-
let converse = body(json!({
|
|
1664
|
-
"messages": [{"role": "user", "content": [{"text": payload}]}]
|
|
1665
|
-
}));
|
|
1666
|
-
let anthropic = body(json!({
|
|
1667
|
-
"messages": [{"role": "user", "content": [{"type": "text", "text": payload}]}]
|
|
1668
|
-
}));
|
|
1669
|
-
let c = compress_anthropic_live_zone(&converse, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1670
|
-
let a = compress_anthropic_live_zone(&anthropic, 0, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1671
|
-
|
|
1672
|
-
// Identical dispatch outcome (both Modified or both NoChange).
|
|
1673
|
-
assert_eq!(
|
|
1674
|
-
std::mem::discriminant(&c),
|
|
1675
|
-
std::mem::discriminant(&a),
|
|
1676
|
-
"converse text block must dispatch like an anthropic text block"
|
|
1677
|
-
);
|
|
1678
|
-
let cm = match &c {
|
|
1679
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1680
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1681
|
-
};
|
|
1682
|
-
let am = match &a {
|
|
1683
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1684
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1685
|
-
};
|
|
1686
|
-
// The Converse block is now classified the same as Anthropic text
|
|
1687
|
-
// (before this change it was an unrecognized typeless block).
|
|
1688
|
-
assert_eq!(cm.block_outcomes.len(), 1);
|
|
1689
|
-
assert_eq!(am.block_outcomes.len(), 1);
|
|
1690
|
-
assert_eq!(
|
|
1691
|
-
cm.block_outcomes[0].block_type,
|
|
1692
|
-
am.block_outcomes[0].block_type
|
|
1693
|
-
);
|
|
1694
|
-
assert_eq!(cm.block_outcomes[0].block_type, "text");
|
|
1695
|
-
}
|
|
1696
|
-
|
|
1697
|
-
#[test]
|
|
1698
|
-
fn manifest_records_messages_below_floor() {
|
|
1699
|
-
let b = body(json!({
|
|
1700
|
-
"messages": [
|
|
1701
|
-
{"role": "user", "content": "frozen"},
|
|
1702
|
-
{"role": "assistant", "content": "frozen"},
|
|
1703
|
-
{"role": "user", "content": "live"},
|
|
1704
|
-
]
|
|
1705
|
-
}));
|
|
1706
|
-
let out = compress_anthropic_live_zone(&b, 2, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1707
|
-
let manifest = match &out {
|
|
1708
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1709
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
1710
|
-
};
|
|
1711
|
-
assert_eq!(manifest.messages_total, 3);
|
|
1712
|
-
assert_eq!(manifest.messages_below_frozen_floor, 2);
|
|
1713
|
-
assert_eq!(manifest.latest_user_message_index, Some(2));
|
|
1714
|
-
}
|
|
1715
|
-
|
|
1716
|
-
#[test]
|
|
1717
|
-
fn frozen_count_above_messages_clamps() {
|
|
1718
|
-
let b = body(json!({
|
|
1719
|
-
"messages": [{"role": "user", "content": "x"}]
|
|
1720
|
-
}));
|
|
1721
|
-
let out = compress_anthropic_live_zone(&b, 99, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
1722
|
-
let manifest = match &out {
|
|
1723
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
1724
|
-
_ => panic!("expected NoChange"),
|
|
1725
|
-
};
|
|
1726
|
-
assert_eq!(manifest.messages_below_frozen_floor, 1);
|
|
1727
|
-
assert_eq!(manifest.latest_user_message_index, None);
|
|
1728
|
-
}
|
|
1729
|
-
|
|
1730
|
-
// ─── Manifest accessor helpers (consumed by PyO3 binding) ─────────
|
|
1731
|
-
|
|
1732
|
-
fn make_manifest(actions: Vec<BlockAction>) -> CompressionManifest {
|
|
1733
|
-
CompressionManifest {
|
|
1734
|
-
messages_total: actions.len(),
|
|
1735
|
-
messages_below_frozen_floor: 0,
|
|
1736
|
-
latest_user_message_index: None,
|
|
1737
|
-
block_outcomes: actions
|
|
1738
|
-
.into_iter()
|
|
1739
|
-
.enumerate()
|
|
1740
|
-
.map(|(i, a)| BlockOutcome {
|
|
1741
|
-
message_index: i,
|
|
1742
|
-
block_index: None,
|
|
1743
|
-
block_type: "test".to_string(),
|
|
1744
|
-
action: a,
|
|
1745
|
-
})
|
|
1746
|
-
.collect(),
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
|
|
1750
|
-
#[test]
|
|
1751
|
-
fn tokens_saved_zero_for_empty_manifest() {
|
|
1752
|
-
let m = CompressionManifest::empty();
|
|
1753
|
-
assert_eq!(m.tokens_saved(), 0);
|
|
1754
|
-
assert!(m.transforms_applied().is_empty());
|
|
1755
|
-
}
|
|
1756
|
-
|
|
1757
|
-
#[test]
|
|
1758
|
-
fn tokens_saved_sums_compressed_outcomes_only() {
|
|
1759
|
-
let m = make_manifest(vec![
|
|
1760
|
-
BlockAction::Compressed {
|
|
1761
|
-
strategy: "smart_crusher",
|
|
1762
|
-
original_bytes: 0,
|
|
1763
|
-
compressed_bytes: 0,
|
|
1764
|
-
original_tokens: 100,
|
|
1765
|
-
compressed_tokens: 30,
|
|
1766
|
-
},
|
|
1767
|
-
BlockAction::NoCompressionApplied {
|
|
1768
|
-
content_type: "image".to_string(),
|
|
1769
|
-
},
|
|
1770
|
-
BlockAction::Compressed {
|
|
1771
|
-
strategy: "log_compressor",
|
|
1772
|
-
original_bytes: 0,
|
|
1773
|
-
compressed_bytes: 0,
|
|
1774
|
-
original_tokens: 200,
|
|
1775
|
-
compressed_tokens: 50,
|
|
1776
|
-
},
|
|
1777
|
-
BlockAction::RejectedNotSmaller {
|
|
1778
|
-
strategy: "smart_crusher",
|
|
1779
|
-
original_bytes: 0,
|
|
1780
|
-
compressed_bytes: 0,
|
|
1781
|
-
original_tokens: 80,
|
|
1782
|
-
compressed_tokens: 90,
|
|
1783
|
-
},
|
|
1784
|
-
]);
|
|
1785
|
-
// 70 + 150 = 220; rejected variant must not contribute.
|
|
1786
|
-
assert_eq!(m.tokens_saved(), 220);
|
|
1787
|
-
}
|
|
1788
|
-
|
|
1789
|
-
#[test]
|
|
1790
|
-
fn transforms_applied_dedup_first_seen_order() {
|
|
1791
|
-
let m = make_manifest(vec![
|
|
1792
|
-
BlockAction::Compressed {
|
|
1793
|
-
strategy: "log_compressor",
|
|
1794
|
-
original_bytes: 0,
|
|
1795
|
-
compressed_bytes: 0,
|
|
1796
|
-
original_tokens: 50,
|
|
1797
|
-
compressed_tokens: 10,
|
|
1798
|
-
},
|
|
1799
|
-
BlockAction::Compressed {
|
|
1800
|
-
strategy: "smart_crusher",
|
|
1801
|
-
original_bytes: 0,
|
|
1802
|
-
compressed_bytes: 0,
|
|
1803
|
-
original_tokens: 50,
|
|
1804
|
-
compressed_tokens: 10,
|
|
1805
|
-
},
|
|
1806
|
-
BlockAction::Compressed {
|
|
1807
|
-
strategy: "log_compressor",
|
|
1808
|
-
original_bytes: 0,
|
|
1809
|
-
compressed_bytes: 0,
|
|
1810
|
-
original_tokens: 50,
|
|
1811
|
-
compressed_tokens: 10,
|
|
1812
|
-
},
|
|
1813
|
-
]);
|
|
1814
|
-
assert_eq!(
|
|
1815
|
-
m.transforms_applied(),
|
|
1816
|
-
vec!["log_compressor", "smart_crusher"]
|
|
1817
|
-
);
|
|
1818
|
-
}
|
|
1819
|
-
|
|
1820
|
-
#[test]
|
|
1821
|
-
fn tokens_saved_saturates_when_compressed_exceeds_original() {
|
|
1822
|
-
// Defensive — the dispatcher's RejectedNotSmaller gate should
|
|
1823
|
-
// make this unreachable, but the helper must not panic if a
|
|
1824
|
-
// future caller hand-constructs such a manifest.
|
|
1825
|
-
let m = make_manifest(vec![BlockAction::Compressed {
|
|
1826
|
-
strategy: "smart_crusher",
|
|
1827
|
-
original_bytes: 0,
|
|
1828
|
-
compressed_bytes: 0,
|
|
1829
|
-
original_tokens: 10,
|
|
1830
|
-
compressed_tokens: 50,
|
|
1831
|
-
}]);
|
|
1832
|
-
assert_eq!(m.tokens_saved(), 0);
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
|
|
1836
|
-
// ─── OpenAI Chat Completions live-zone dispatcher (Phase C PR-C2) ────────
|
|
1837
|
-
//
|
|
1838
|
-
// Sibling of `compress_anthropic_live_zone`. Same compressor backend,
|
|
1839
|
-
// same per-content-type byte thresholds, same tokenizer-validated
|
|
1840
|
-
// rejection gate, same byte-range-surgery rewrite strategy. The
|
|
1841
|
-
// difference is the walker: Chat Completions defines the live zone as
|
|
1842
|
-
// the LATEST `role: "tool"` message and the LATEST `role: "user"`
|
|
1843
|
-
// message (separately, not as a contiguous run). All earlier `tool` /
|
|
1844
|
-
// `user` messages are part of the cache hot zone — never touched.
|
|
1845
|
-
//
|
|
1846
|
-
// Tool messages have shape `{role: "tool", tool_call_id, content}`
|
|
1847
|
-
// where `content` is either a JSON string (the common case) or an
|
|
1848
|
-
// array of content parts (rarer; only the string shape is compressible).
|
|
1849
|
-
// User messages have shape `{role: "user", content}` where `content`
|
|
1850
|
-
// is either a JSON string or an array of `{type: "text", text}` /
|
|
1851
|
-
// `{type: "image_url", ...}` blocks; only the text-blocks are eligible.
|
|
1852
|
-
//
|
|
1853
|
-
// `n > 1` (multiple completions) is gated *outside* this function by
|
|
1854
|
-
// the proxy handler — we keep the dispatcher pure and unaware of
|
|
1855
|
-
// non-determinism semantics.
|
|
1856
|
-
|
|
1857
|
-
/// Compress live-zone blocks of an OpenAI Chat Completions request.
|
|
1858
|
-
///
|
|
1859
|
-
/// # Provider scope
|
|
1860
|
-
///
|
|
1861
|
-
/// `/v1/chat/completions` only. The body shape is:
|
|
1862
|
-
///
|
|
1863
|
-
/// ```json
|
|
1864
|
-
/// { "model": "...", "messages": [ {"role": "...", "content": "..."}, ... ] }
|
|
1865
|
-
/// ```
|
|
1866
|
-
///
|
|
1867
|
-
/// Live zone = the latest `tool` role message's `content` plus the
|
|
1868
|
-
/// latest `user` role message's text content. Earlier `tool` and
|
|
1869
|
-
/// `user` messages are frozen (cached prefix); never rewritten.
|
|
1870
|
-
///
|
|
1871
|
-
/// Cache-safety invariant matches the Anthropic dispatcher: bytes
|
|
1872
|
-
/// outside the rewritten ranges are *literally copied* from the input,
|
|
1873
|
-
/// never re-serialized. PR-C2 integration tests pin SHA-256 byte
|
|
1874
|
-
/// equality on the prefix and suffix.
|
|
1875
|
-
pub fn compress_openai_chat_live_zone(
|
|
1876
|
-
body_raw: &[u8],
|
|
1877
|
-
_auth_mode: AuthMode,
|
|
1878
|
-
model: &str,
|
|
1879
|
-
) -> Result<LiveZoneOutcome, LiveZoneError> {
|
|
1880
|
-
let parsed: Value = serde_json::from_slice(body_raw).map_err(LiveZoneError::BodyNotJson)?;
|
|
1881
|
-
let messages = parsed
|
|
1882
|
-
.get("messages")
|
|
1883
|
-
.and_then(Value::as_array)
|
|
1884
|
-
.ok_or(LiveZoneError::NoMessagesArray)?;
|
|
1885
|
-
|
|
1886
|
-
if messages.is_empty() {
|
|
1887
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
1888
|
-
manifest: CompressionManifest::empty(),
|
|
1889
|
-
});
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
let messages_total = messages.len();
|
|
1893
|
-
|
|
1894
|
-
// Latest tool / user message indices in the live zone.
|
|
1895
|
-
let latest_tool_idx = find_latest_role_index(messages, "tool");
|
|
1896
|
-
let latest_user_idx = find_latest_role_index(messages, "user");
|
|
1897
|
-
|
|
1898
|
-
// No live-zone candidates → NoChange.
|
|
1899
|
-
if latest_tool_idx.is_none() && latest_user_idx.is_none() {
|
|
1900
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
1901
|
-
manifest: CompressionManifest {
|
|
1902
|
-
messages_total,
|
|
1903
|
-
messages_below_frozen_floor: 0,
|
|
1904
|
-
latest_user_message_index: latest_user_idx,
|
|
1905
|
-
block_outcomes: Vec::new(),
|
|
1906
|
-
},
|
|
1907
|
-
});
|
|
1908
|
-
}
|
|
1909
|
-
|
|
1910
|
-
// Plan replacements for both targets. Each plan returns slots for
|
|
1911
|
-
// its own message; we stitch them together into a single
|
|
1912
|
-
// replacement vec keyed by ascending byte offset (apply_replacements
|
|
1913
|
-
// sorts defensively too).
|
|
1914
|
-
let mut all_slots: Vec<(usize, OpenAiPlanSlot)> = Vec::new();
|
|
1915
|
-
if let Some(idx) = latest_tool_idx {
|
|
1916
|
-
// Body shape doesn't match what we expect → skip planning
|
|
1917
|
-
// for the tool message but keep going for the user message.
|
|
1918
|
-
if let Ok(slot) = plan_openai_tool_message(body_raw, idx) {
|
|
1919
|
-
all_slots.push((idx, slot));
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
if let Some(idx) = latest_user_idx {
|
|
1923
|
-
if let Ok(slots) = plan_openai_user_message(body_raw, idx) {
|
|
1924
|
-
for s in slots {
|
|
1925
|
-
all_slots.push((idx, s));
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
}
|
|
1929
|
-
|
|
1930
|
-
if all_slots.is_empty() {
|
|
1931
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
1932
|
-
manifest: CompressionManifest {
|
|
1933
|
-
messages_total,
|
|
1934
|
-
messages_below_frozen_floor: 0,
|
|
1935
|
-
latest_user_message_index: latest_user_idx,
|
|
1936
|
-
block_outcomes: Vec::new(),
|
|
1937
|
-
},
|
|
1938
|
-
});
|
|
1939
|
-
}
|
|
1940
|
-
|
|
1941
|
-
let tokenizer = get_tokenizer(model);
|
|
1942
|
-
let mut block_outcomes: Vec<BlockOutcome> = Vec::with_capacity(all_slots.len());
|
|
1943
|
-
let mut replacements: Vec<Replacement> = Vec::new();
|
|
1944
|
-
|
|
1945
|
-
for (msg_idx, slot) in all_slots {
|
|
1946
|
-
let detected = detect_content_type(&slot.content_text);
|
|
1947
|
-
let outcome = compress_one_block(
|
|
1948
|
-
&slot.content_text,
|
|
1949
|
-
detected.content_type,
|
|
1950
|
-
slot.content_byte_range,
|
|
1951
|
-
msg_idx,
|
|
1952
|
-
slot.block_index,
|
|
1953
|
-
slot.block_type,
|
|
1954
|
-
tokenizer.as_ref(),
|
|
1955
|
-
&mut replacements,
|
|
1956
|
-
None, // PR-C2: no CCR store yet on the OpenAI path.
|
|
1957
|
-
);
|
|
1958
|
-
block_outcomes.push(outcome);
|
|
1959
|
-
}
|
|
1960
|
-
|
|
1961
|
-
let manifest = CompressionManifest {
|
|
1962
|
-
messages_total,
|
|
1963
|
-
messages_below_frozen_floor: 0,
|
|
1964
|
-
latest_user_message_index: latest_user_idx,
|
|
1965
|
-
block_outcomes,
|
|
1966
|
-
};
|
|
1967
|
-
|
|
1968
|
-
if !manifest.has_compressed_block() || replacements.is_empty() {
|
|
1969
|
-
return Ok(LiveZoneOutcome::NoChange { manifest });
|
|
1970
|
-
}
|
|
1971
|
-
|
|
1972
|
-
let new_bytes = apply_replacements(body_raw, &mut replacements);
|
|
1973
|
-
let new_body_str = match std::str::from_utf8(&new_bytes) {
|
|
1974
|
-
Ok(s) => s,
|
|
1975
|
-
Err(_) => return Ok(LiveZoneOutcome::NoChange { manifest }),
|
|
1976
|
-
};
|
|
1977
|
-
let raw = match RawValue::from_string(new_body_str.to_string()) {
|
|
1978
|
-
Ok(r) => r,
|
|
1979
|
-
Err(_) => return Ok(LiveZoneOutcome::NoChange { manifest }),
|
|
1980
|
-
};
|
|
1981
|
-
|
|
1982
|
-
Ok(LiveZoneOutcome::Modified {
|
|
1983
|
-
new_body: raw,
|
|
1984
|
-
manifest,
|
|
1985
|
-
})
|
|
1986
|
-
}
|
|
1987
|
-
|
|
1988
|
-
/// Find the highest index of a message with `role == role`. `None` if
|
|
1989
|
-
/// no such message exists.
|
|
1990
|
-
fn find_latest_role_index(messages: &[Value], role: &str) -> Option<usize> {
|
|
1991
|
-
for (idx, msg) in messages.iter().enumerate().rev() {
|
|
1992
|
-
if msg.get("role").and_then(Value::as_str) == Some(role) {
|
|
1993
|
-
return Some(idx);
|
|
1994
|
-
}
|
|
1995
|
-
}
|
|
1996
|
-
None
|
|
1997
|
-
}
|
|
1998
|
-
|
|
1999
|
-
/// One OpenAI live-zone plan slot. Mirrors `PlanSlot` but emits the
|
|
2000
|
-
/// `block_index` and `block_type` shape `compress_one_block` expects.
|
|
2001
|
-
struct OpenAiPlanSlot {
|
|
2002
|
-
block_index: Option<usize>,
|
|
2003
|
-
block_type: String,
|
|
2004
|
-
content_text: String,
|
|
2005
|
-
content_byte_range: (usize, usize),
|
|
2006
|
-
}
|
|
2007
|
-
|
|
2008
|
-
/// Plan a replacement slot for the tool message at `msg_idx`. Tool
|
|
2009
|
-
/// messages carry `content` as either a string (compressible) or an
|
|
2010
|
-
/// array of parts (rare; not compressed in PR-C2 — falls through).
|
|
2011
|
-
fn plan_openai_tool_message(body_raw: &[u8], msg_idx: usize) -> Result<OpenAiPlanSlot, PlanError> {
|
|
2012
|
-
let body_str = std::str::from_utf8(body_raw).map_err(|_| PlanError::ParseFailed)?;
|
|
2013
|
-
let body: BodyView<'_> = serde_json::from_str(body_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2014
|
-
let msg_raw = body
|
|
2015
|
-
.messages
|
|
2016
|
-
.get(msg_idx)
|
|
2017
|
-
.ok_or(PlanError::TargetOutOfBounds)?;
|
|
2018
|
-
|
|
2019
|
-
let msg_view: MessageView<'_> =
|
|
2020
|
-
serde_json::from_str(msg_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2021
|
-
let content_raw = msg_view.content.ok_or(PlanError::ParseFailed)?;
|
|
2022
|
-
|
|
2023
|
-
let content_offset_in_msg =
|
|
2024
|
-
bytes_offset_of(msg_raw.get(), content_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2025
|
-
let msg_offset_in_body =
|
|
2026
|
-
bytes_offset_of(body_str, msg_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2027
|
-
let content_offset_in_body = msg_offset_in_body + content_offset_in_msg;
|
|
2028
|
-
|
|
2029
|
-
let content_str = content_raw.get();
|
|
2030
|
-
if !content_str.starts_with('"') {
|
|
2031
|
-
// Non-string content (array of parts). PR-C2 doesn't walk
|
|
2032
|
-
// these — treat as not-planned and let the dispatcher record
|
|
2033
|
-
// no slot. This is a planner-level skip, not a parse error.
|
|
2034
|
-
return Err(PlanError::ParseFailed);
|
|
2035
|
-
}
|
|
2036
|
-
|
|
2037
|
-
let unescaped: String =
|
|
2038
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2039
|
-
|
|
2040
|
-
Ok(OpenAiPlanSlot {
|
|
2041
|
-
block_index: None,
|
|
2042
|
-
block_type: "tool_content".to_string(),
|
|
2043
|
-
content_text: unescaped,
|
|
2044
|
-
content_byte_range: (
|
|
2045
|
-
content_offset_in_body,
|
|
2046
|
-
content_offset_in_body + content_str.len(),
|
|
2047
|
-
),
|
|
2048
|
-
})
|
|
2049
|
-
}
|
|
2050
|
-
|
|
2051
|
-
/// Plan replacement slots for the user message at `msg_idx`. User
|
|
2052
|
-
/// content can be:
|
|
2053
|
-
///
|
|
2054
|
-
/// - A JSON string → compressible as a single slot.
|
|
2055
|
-
/// - An array of parts where each `{type: "text", text}` is a
|
|
2056
|
-
/// compressible slot. `{type: "image_url", ...}` and other
|
|
2057
|
-
/// non-text parts are skipped.
|
|
2058
|
-
fn plan_openai_user_message(
|
|
2059
|
-
body_raw: &[u8],
|
|
2060
|
-
msg_idx: usize,
|
|
2061
|
-
) -> Result<Vec<OpenAiPlanSlot>, PlanError> {
|
|
2062
|
-
let body_str = std::str::from_utf8(body_raw).map_err(|_| PlanError::ParseFailed)?;
|
|
2063
|
-
let body: BodyView<'_> = serde_json::from_str(body_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2064
|
-
let msg_raw = body
|
|
2065
|
-
.messages
|
|
2066
|
-
.get(msg_idx)
|
|
2067
|
-
.ok_or(PlanError::TargetOutOfBounds)?;
|
|
2068
|
-
|
|
2069
|
-
let msg_view: MessageView<'_> =
|
|
2070
|
-
serde_json::from_str(msg_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2071
|
-
let Some(content_raw) = msg_view.content else {
|
|
2072
|
-
return Ok(Vec::new());
|
|
2073
|
-
};
|
|
2074
|
-
|
|
2075
|
-
let content_offset_in_msg =
|
|
2076
|
-
bytes_offset_of(msg_raw.get(), content_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2077
|
-
let msg_offset_in_body =
|
|
2078
|
-
bytes_offset_of(body_str, msg_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2079
|
-
let content_offset_in_body = msg_offset_in_body + content_offset_in_msg;
|
|
2080
|
-
|
|
2081
|
-
let content_str = content_raw.get();
|
|
2082
|
-
|
|
2083
|
-
// Case 1: content is a JSON string.
|
|
2084
|
-
if content_str.starts_with('"') {
|
|
2085
|
-
let unescaped: String =
|
|
2086
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2087
|
-
return Ok(vec![OpenAiPlanSlot {
|
|
2088
|
-
block_index: None,
|
|
2089
|
-
block_type: "user_string".to_string(),
|
|
2090
|
-
content_text: unescaped,
|
|
2091
|
-
content_byte_range: (
|
|
2092
|
-
content_offset_in_body,
|
|
2093
|
-
content_offset_in_body + content_str.len(),
|
|
2094
|
-
),
|
|
2095
|
-
}]);
|
|
2096
|
-
}
|
|
2097
|
-
|
|
2098
|
-
// Case 2: content is an array of typed parts.
|
|
2099
|
-
let parts: Vec<&RawValue> =
|
|
2100
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2101
|
-
|
|
2102
|
-
let mut slots = Vec::with_capacity(parts.len());
|
|
2103
|
-
for (part_idx, part_raw) in parts.iter().enumerate() {
|
|
2104
|
-
let header: BlockHeader<'_> =
|
|
2105
|
-
serde_json::from_str(part_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2106
|
-
let block_type = header.r#type.unwrap_or("unknown").to_string();
|
|
2107
|
-
if block_type != "text" {
|
|
2108
|
-
// Skip image_url / other non-text parts.
|
|
2109
|
-
continue;
|
|
2110
|
-
}
|
|
2111
|
-
|
|
2112
|
-
// Extract the `text` field byte range.
|
|
2113
|
-
#[derive(Deserialize)]
|
|
2114
|
-
struct TextHeader<'a> {
|
|
2115
|
-
#[serde(borrow, default)]
|
|
2116
|
-
text: Option<&'a RawValue>,
|
|
2117
|
-
}
|
|
2118
|
-
let h: TextHeader<'_> =
|
|
2119
|
-
serde_json::from_str(part_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2120
|
-
let Some(text_raw) = h.text else {
|
|
2121
|
-
continue;
|
|
2122
|
-
};
|
|
2123
|
-
|
|
2124
|
-
let part_offset_in_content =
|
|
2125
|
-
bytes_offset_of(content_str, part_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2126
|
-
let part_offset_in_body = content_offset_in_body + part_offset_in_content;
|
|
2127
|
-
let text_offset_in_part =
|
|
2128
|
-
bytes_offset_of(part_raw.get(), text_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2129
|
-
|
|
2130
|
-
let text_str = text_raw.get();
|
|
2131
|
-
if !text_str.starts_with('"') {
|
|
2132
|
-
continue;
|
|
2133
|
-
}
|
|
2134
|
-
let unescaped: String =
|
|
2135
|
-
serde_json::from_str(text_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2136
|
-
|
|
2137
|
-
let text_start_in_body = part_offset_in_body + text_offset_in_part;
|
|
2138
|
-
let text_end_in_body = text_start_in_body + text_str.len();
|
|
2139
|
-
|
|
2140
|
-
slots.push(OpenAiPlanSlot {
|
|
2141
|
-
block_index: Some(part_idx),
|
|
2142
|
-
block_type: "user_text".to_string(),
|
|
2143
|
-
content_text: unescaped,
|
|
2144
|
-
content_byte_range: (text_start_in_body, text_end_in_body),
|
|
2145
|
-
});
|
|
2146
|
-
}
|
|
2147
|
-
|
|
2148
|
-
Ok(slots)
|
|
2149
|
-
}
|
|
2150
|
-
|
|
2151
|
-
#[cfg(test)]
|
|
2152
|
-
mod openai_chat_tests {
|
|
2153
|
-
use super::*;
|
|
2154
|
-
use serde_json::json;
|
|
2155
|
-
|
|
2156
|
-
fn body(value: Value) -> Vec<u8> {
|
|
2157
|
-
serde_json::to_vec(&value).unwrap()
|
|
2158
|
-
}
|
|
2159
|
-
|
|
2160
|
-
#[test]
|
|
2161
|
-
fn empty_messages_yields_no_change() {
|
|
2162
|
-
let b = body(json!({"model": "gpt-4o", "messages": []}));
|
|
2163
|
-
let out = compress_openai_chat_live_zone(&b, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
2164
|
-
assert!(matches!(out, LiveZoneOutcome::NoChange { .. }));
|
|
2165
|
-
}
|
|
2166
|
-
|
|
2167
|
-
#[test]
|
|
2168
|
-
fn no_messages_field_errors() {
|
|
2169
|
-
let b = body(json!({"model": "gpt-4o"}));
|
|
2170
|
-
let err = compress_openai_chat_live_zone(&b, AuthMode::Payg, DEFAULT_MODEL).unwrap_err();
|
|
2171
|
-
assert!(matches!(err, LiveZoneError::NoMessagesArray));
|
|
2172
|
-
}
|
|
2173
|
-
|
|
2174
|
-
#[test]
|
|
2175
|
-
fn invalid_json_errors() {
|
|
2176
|
-
let err =
|
|
2177
|
-
compress_openai_chat_live_zone(b"not json", AuthMode::Payg, DEFAULT_MODEL).unwrap_err();
|
|
2178
|
-
assert!(matches!(err, LiveZoneError::BodyNotJson(_)));
|
|
2179
|
-
}
|
|
2180
|
-
|
|
2181
|
-
#[test]
|
|
2182
|
-
fn no_user_or_tool_yields_no_change() {
|
|
2183
|
-
let b = body(json!({
|
|
2184
|
-
"messages": [{"role": "system", "content": "you are helpful"}]
|
|
2185
|
-
}));
|
|
2186
|
-
let out = compress_openai_chat_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2187
|
-
assert!(matches!(out, LiveZoneOutcome::NoChange { .. }));
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
#[test]
|
|
2191
|
-
fn tiny_tool_content_below_threshold_no_change() {
|
|
2192
|
-
let b = body(json!({
|
|
2193
|
-
"messages": [
|
|
2194
|
-
{"role": "user", "content": "hi"},
|
|
2195
|
-
{"role": "assistant", "content": "doing tool"},
|
|
2196
|
-
{"role": "tool", "tool_call_id": "t1", "content": "ok"},
|
|
2197
|
-
]
|
|
2198
|
-
}));
|
|
2199
|
-
let out = compress_openai_chat_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2200
|
-
match &out {
|
|
2201
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2202
|
-
// Both latest tool (idx 2) and latest user (idx 0)
|
|
2203
|
-
// contributed a slot; both below threshold.
|
|
2204
|
-
assert!(manifest
|
|
2205
|
-
.block_outcomes
|
|
2206
|
-
.iter()
|
|
2207
|
-
.all(|b| matches!(b.action, BlockAction::BelowByteThreshold { .. })));
|
|
2208
|
-
}
|
|
2209
|
-
_ => panic!("expected NoChange"),
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
|
|
2213
|
-
#[test]
|
|
2214
|
-
fn user_array_text_parts_planned() {
|
|
2215
|
-
// User content as array of {type: text} + {type: image_url}.
|
|
2216
|
-
// Only the text part is planned.
|
|
2217
|
-
let b = body(json!({
|
|
2218
|
-
"messages": [{
|
|
2219
|
-
"role": "user",
|
|
2220
|
-
"content": [
|
|
2221
|
-
{"type": "text", "text": "describe this"},
|
|
2222
|
-
{"type": "image_url", "image_url": {"url": "data:..."}},
|
|
2223
|
-
]
|
|
2224
|
-
}]
|
|
2225
|
-
}));
|
|
2226
|
-
let out = compress_openai_chat_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2227
|
-
match &out {
|
|
2228
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2229
|
-
assert_eq!(manifest.block_outcomes.len(), 1);
|
|
2230
|
-
assert_eq!(manifest.block_outcomes[0].block_type, "user_text");
|
|
2231
|
-
}
|
|
2232
|
-
_ => panic!("expected NoChange"),
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
|
|
2236
|
-
#[test]
|
|
2237
|
-
fn picks_latest_tool_only() {
|
|
2238
|
-
// Two tool messages; only the latest is in the live zone.
|
|
2239
|
-
let b = body(json!({
|
|
2240
|
-
"messages": [
|
|
2241
|
-
{"role": "user", "content": "hi"},
|
|
2242
|
-
{"role": "tool", "tool_call_id": "t1", "content": "early"},
|
|
2243
|
-
{"role": "user", "content": "again"},
|
|
2244
|
-
{"role": "tool", "tool_call_id": "t2", "content": "late"},
|
|
2245
|
-
]
|
|
2246
|
-
}));
|
|
2247
|
-
let out = compress_openai_chat_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2248
|
-
let manifest = match &out {
|
|
2249
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
2250
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
2251
|
-
};
|
|
2252
|
-
// Tool block should reference message index 3 (latest tool),
|
|
2253
|
-
// user block index 2 (latest user).
|
|
2254
|
-
let tool_block = manifest
|
|
2255
|
-
.block_outcomes
|
|
2256
|
-
.iter()
|
|
2257
|
-
.find(|b| b.block_type == "tool_content")
|
|
2258
|
-
.expect("tool block recorded");
|
|
2259
|
-
assert_eq!(tool_block.message_index, 3);
|
|
2260
|
-
let user_block = manifest
|
|
2261
|
-
.block_outcomes
|
|
2262
|
-
.iter()
|
|
2263
|
-
.find(|b| b.block_type == "user_string")
|
|
2264
|
-
.expect("user block recorded");
|
|
2265
|
-
assert_eq!(user_block.message_index, 2);
|
|
2266
|
-
}
|
|
2267
|
-
}
|
|
2268
|
-
|
|
2269
|
-
// ─── OpenAI Responses live-zone dispatcher (Phase C PR-C3) ────────────
|
|
2270
|
-
//
|
|
2271
|
-
// Sibling of `compress_openai_chat_live_zone`. The Responses API
|
|
2272
|
-
// (`/v1/responses`) keys the request under `input` rather than
|
|
2273
|
-
// `messages`, and the array carries explicitly-typed items (not
|
|
2274
|
-
// role-tagged messages).
|
|
2275
|
-
//
|
|
2276
|
-
// Live zone, per spec PR-C3 (`REALIGNMENT/05-phase-C-rust-proxy.md`):
|
|
2277
|
-
//
|
|
2278
|
-
// - latest `function_call_output.output`
|
|
2279
|
-
// - latest `local_shell_call_output.output`
|
|
2280
|
-
// - latest `apply_patch_call_output.output`
|
|
2281
|
-
// - latest `message` (text content) OR `user`-role message
|
|
2282
|
-
//
|
|
2283
|
-
// Earlier `*_output` items are FROZEN (cached prefix) — never touched.
|
|
2284
|
-
// All other item types (`reasoning`, `compaction`, `mcp_*`,
|
|
2285
|
-
// `computer_*`, `web_search_call`, `file_search_call`,
|
|
2286
|
-
// `code_interpreter_call`, `image_generation_call`, `tool_search_call`,
|
|
2287
|
-
// `custom_tool_call`, `function_call`, `local_shell_call`,
|
|
2288
|
-
// `apply_patch_call`, future-unknown) are passthrough — the dispatcher
|
|
2289
|
-
// records a `NoCompressionApplied` outcome but never plans a
|
|
2290
|
-
// replacement.
|
|
2291
|
-
//
|
|
2292
|
-
// Output items must additionally clear a 512-byte minimum
|
|
2293
|
-
// 167) before the per-content-type byte threshold even runs.
|
|
2294
|
-
|
|
2295
|
-
/// Output-item floor below which the Responses dispatcher does not
|
|
2296
|
-
/// even attempt compression. Matches
|
|
2297
|
-
/// `responses_items::OUTPUT_ITEM_MIN_BYTES`; pinned here too because
|
|
2298
|
-
/// `headroom-core` is independent of the proxy crate.
|
|
2299
|
-
const RESPONSES_OUTPUT_MIN_BYTES: usize = 512;
|
|
2300
|
-
|
|
2301
|
-
/// Compress live-zone blocks of an OpenAI Responses request.
|
|
2302
|
-
///
|
|
2303
|
-
/// # Provider scope
|
|
2304
|
-
///
|
|
2305
|
-
/// `/v1/responses` only. The body shape is:
|
|
2306
|
-
///
|
|
2307
|
-
/// ```json
|
|
2308
|
-
/// {
|
|
2309
|
-
/// "model": "...",
|
|
2310
|
-
/// "input": [
|
|
2311
|
-
/// {"type": "message", "role": "user", "content": "..."},
|
|
2312
|
-
/// {"type": "function_call", "call_id": "c1", "name": "...", "arguments": "..."},
|
|
2313
|
-
/// {"type": "function_call_output", "call_id": "c1", "output": "..."},
|
|
2314
|
-
/// {"type": "local_shell_call", ...},
|
|
2315
|
-
/// {"type": "apply_patch_call", "operation": {...}},
|
|
2316
|
-
/// ...
|
|
2317
|
-
/// ]
|
|
2318
|
-
/// }
|
|
2319
|
-
/// ```
|
|
2320
|
-
///
|
|
2321
|
-
/// Live zone = every current-frame output item with a byte-safe
|
|
2322
|
-
/// string payload (`function_call_output`, `local_shell_call_output`,
|
|
2323
|
-
/// `apply_patch_call_output`), except CCR retrieval outputs that must
|
|
2324
|
-
/// reach the model byte-for-byte.
|
|
2325
|
-
/// Codex commonly batches parallel tool results in one `response.create`
|
|
2326
|
-
/// frame; those sibling outputs are all live input for the next model
|
|
2327
|
-
/// turn. All other item types pass through verbatim.
|
|
2328
|
-
///
|
|
2329
|
-
/// Cache-safety invariant matches the Anthropic / Chat dispatchers:
|
|
2330
|
-
/// bytes outside the rewritten ranges are *literally copied* from the
|
|
2331
|
-
/// input, never re-serialized.
|
|
2332
|
-
pub fn compress_openai_responses_live_zone(
|
|
2333
|
-
body_raw: &[u8],
|
|
2334
|
-
_auth_mode: AuthMode,
|
|
2335
|
-
model: &str,
|
|
2336
|
-
) -> Result<LiveZoneOutcome, LiveZoneError> {
|
|
2337
|
-
let parsed: Value = serde_json::from_slice(body_raw).map_err(LiveZoneError::BodyNotJson)?;
|
|
2338
|
-
|
|
2339
|
-
// Responses uses `input`. We accept both `input` and `messages`
|
|
2340
|
-
// for forward-compat (some clients alias) — but `input` is the
|
|
2341
|
-
// canonical name. If neither field is present, surface
|
|
2342
|
-
// `NoMessagesArray` so the proxy can passthrough with a named
|
|
2343
|
-
// reason.
|
|
2344
|
-
let items = parsed
|
|
2345
|
-
.get("input")
|
|
2346
|
-
.or_else(|| parsed.get("messages"))
|
|
2347
|
-
.and_then(Value::as_array)
|
|
2348
|
-
.ok_or(LiveZoneError::NoMessagesArray)?;
|
|
2349
|
-
|
|
2350
|
-
if items.is_empty() {
|
|
2351
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
2352
|
-
manifest: CompressionManifest::empty(),
|
|
2353
|
-
});
|
|
2354
|
-
}
|
|
2355
|
-
|
|
2356
|
-
let items_total = items.len();
|
|
2357
|
-
|
|
2358
|
-
// Output items in the current Responses frame are live deltas, not
|
|
2359
|
-
// cached history. Codex often sends several sibling tool outputs
|
|
2360
|
-
// after parallel local commands; compressing only the last one
|
|
2361
|
-
// leaves large same-frame payloads untouched.
|
|
2362
|
-
let mut headroom_retrieve_call_ids: HashSet<&str> = HashSet::new();
|
|
2363
|
-
for item in items {
|
|
2364
|
-
if item.get("type").and_then(Value::as_str) != Some("function_call") {
|
|
2365
|
-
continue;
|
|
2366
|
-
}
|
|
2367
|
-
let name = item.get("name").and_then(Value::as_str).unwrap_or("");
|
|
2368
|
-
if name == "headroom_retrieve" || name.ends_with("__headroom_retrieve") {
|
|
2369
|
-
if let Some(call_id) = item.get("call_id").and_then(Value::as_str) {
|
|
2370
|
-
headroom_retrieve_call_ids.insert(call_id);
|
|
2371
|
-
}
|
|
2372
|
-
}
|
|
2373
|
-
}
|
|
2374
|
-
|
|
2375
|
-
let mut output_candidates: Vec<(usize, &str)> = Vec::new();
|
|
2376
|
-
let latest_message: Option<usize> = None;
|
|
2377
|
-
|
|
2378
|
-
for (idx, item) in items.iter().enumerate() {
|
|
2379
|
-
let type_tag = item.get("type").and_then(Value::as_str).unwrap_or("");
|
|
2380
|
-
match type_tag {
|
|
2381
|
-
"function_call_output" | "local_shell_call_output" | "apply_patch_call_output" => {
|
|
2382
|
-
let call_id = item.get("call_id").and_then(Value::as_str);
|
|
2383
|
-
if call_id.is_some_and(|id| headroom_retrieve_call_ids.contains(id)) {
|
|
2384
|
-
continue;
|
|
2385
|
-
}
|
|
2386
|
-
output_candidates.push((idx, type_tag));
|
|
2387
|
-
}
|
|
2388
|
-
_ => {}
|
|
2389
|
-
}
|
|
2390
|
-
}
|
|
2391
|
-
|
|
2392
|
-
let mut candidates = output_candidates;
|
|
2393
|
-
if let Some(idx) = latest_message {
|
|
2394
|
-
candidates.push((idx, "message"));
|
|
2395
|
-
}
|
|
2396
|
-
|
|
2397
|
-
if candidates.is_empty() {
|
|
2398
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
2399
|
-
manifest: CompressionManifest {
|
|
2400
|
-
messages_total: items_total,
|
|
2401
|
-
messages_below_frozen_floor: 0,
|
|
2402
|
-
latest_user_message_index: latest_message,
|
|
2403
|
-
block_outcomes: Vec::new(),
|
|
2404
|
-
},
|
|
2405
|
-
});
|
|
2406
|
-
}
|
|
2407
|
-
|
|
2408
|
-
// Plan replacements per candidate kind. Each plan returns at most
|
|
2409
|
-
// one slot (output items have a single string field; messages
|
|
2410
|
-
// have a single text content slot).
|
|
2411
|
-
let mut all_slots: Vec<(usize, ResponsesPlanSlot)> = Vec::new();
|
|
2412
|
-
for (idx, kind_tag) in candidates {
|
|
2413
|
-
match plan_responses_item(body_raw, idx, kind_tag) {
|
|
2414
|
-
Ok(Some(slot)) => all_slots.push((idx, slot)),
|
|
2415
|
-
Ok(None) => {}
|
|
2416
|
-
Err(_) => {
|
|
2417
|
-
// Body shape doesn't match what we expect for this
|
|
2418
|
-
// item — skip it but keep going for the others.
|
|
2419
|
-
continue;
|
|
2420
|
-
}
|
|
2421
|
-
}
|
|
2422
|
-
}
|
|
2423
|
-
|
|
2424
|
-
if all_slots.is_empty() {
|
|
2425
|
-
return Ok(LiveZoneOutcome::NoChange {
|
|
2426
|
-
manifest: CompressionManifest {
|
|
2427
|
-
messages_total: items_total,
|
|
2428
|
-
messages_below_frozen_floor: 0,
|
|
2429
|
-
latest_user_message_index: latest_message,
|
|
2430
|
-
block_outcomes: Vec::new(),
|
|
2431
|
-
},
|
|
2432
|
-
});
|
|
2433
|
-
}
|
|
2434
|
-
|
|
2435
|
-
let tokenizer = get_tokenizer(model);
|
|
2436
|
-
let mut block_outcomes: Vec<BlockOutcome> = Vec::with_capacity(all_slots.len());
|
|
2437
|
-
let mut replacements: Vec<Replacement> = Vec::new();
|
|
2438
|
-
|
|
2439
|
-
for (msg_idx, slot) in all_slots {
|
|
2440
|
-
// Output items must clear the response-output floor BEFORE the
|
|
2441
|
-
// per-content-type threshold even runs. This is on top of the
|
|
2442
|
-
// existing per-block byte-threshold gate.
|
|
2443
|
-
if slot.is_output_item && slot.content_text.len() < RESPONSES_OUTPUT_MIN_BYTES {
|
|
2444
|
-
block_outcomes.push(BlockOutcome {
|
|
2445
|
-
message_index: msg_idx,
|
|
2446
|
-
block_index: slot.block_index,
|
|
2447
|
-
block_type: slot.block_type.clone(),
|
|
2448
|
-
action: BlockAction::BelowByteThreshold {
|
|
2449
|
-
content_type: "output_item",
|
|
2450
|
-
byte_count: slot.content_text.len(),
|
|
2451
|
-
threshold_bytes: RESPONSES_OUTPUT_MIN_BYTES,
|
|
2452
|
-
},
|
|
2453
|
-
});
|
|
2454
|
-
continue;
|
|
2455
|
-
}
|
|
2456
|
-
let detected = detect_content_type(&slot.content_text);
|
|
2457
|
-
let outcome = compress_one_block(
|
|
2458
|
-
&slot.content_text,
|
|
2459
|
-
detected.content_type,
|
|
2460
|
-
slot.content_byte_range,
|
|
2461
|
-
msg_idx,
|
|
2462
|
-
slot.block_index,
|
|
2463
|
-
slot.block_type,
|
|
2464
|
-
tokenizer.as_ref(),
|
|
2465
|
-
&mut replacements,
|
|
2466
|
-
None, // PR-C3: no CCR store on the Responses path yet.
|
|
2467
|
-
);
|
|
2468
|
-
block_outcomes.push(outcome);
|
|
2469
|
-
}
|
|
2470
|
-
|
|
2471
|
-
let manifest = CompressionManifest {
|
|
2472
|
-
messages_total: items_total,
|
|
2473
|
-
messages_below_frozen_floor: 0,
|
|
2474
|
-
latest_user_message_index: latest_message,
|
|
2475
|
-
block_outcomes,
|
|
2476
|
-
};
|
|
2477
|
-
|
|
2478
|
-
if !manifest.has_compressed_block() || replacements.is_empty() {
|
|
2479
|
-
return Ok(LiveZoneOutcome::NoChange { manifest });
|
|
2480
|
-
}
|
|
2481
|
-
|
|
2482
|
-
let new_bytes = apply_replacements(body_raw, &mut replacements);
|
|
2483
|
-
let new_body_str = match std::str::from_utf8(&new_bytes) {
|
|
2484
|
-
Ok(s) => s,
|
|
2485
|
-
Err(_) => return Ok(LiveZoneOutcome::NoChange { manifest }),
|
|
2486
|
-
};
|
|
2487
|
-
let raw = match RawValue::from_string(new_body_str.to_string()) {
|
|
2488
|
-
Ok(r) => r,
|
|
2489
|
-
Err(_) => return Ok(LiveZoneOutcome::NoChange { manifest }),
|
|
2490
|
-
};
|
|
2491
|
-
|
|
2492
|
-
Ok(LiveZoneOutcome::Modified {
|
|
2493
|
-
new_body: raw,
|
|
2494
|
-
manifest,
|
|
2495
|
-
})
|
|
2496
|
-
}
|
|
2497
|
-
|
|
2498
|
-
/// Per-kind plan slot for the Responses dispatcher. Mirrors
|
|
2499
|
-
/// `OpenAiPlanSlot` but tracks whether the slot is an `*_output` item
|
|
2500
|
-
/// (so the response-output floor only applies there, not to `message` text).
|
|
2501
|
-
struct ResponsesPlanSlot {
|
|
2502
|
-
block_index: Option<usize>,
|
|
2503
|
-
block_type: String,
|
|
2504
|
-
content_text: String,
|
|
2505
|
-
content_byte_range: (usize, usize),
|
|
2506
|
-
/// True when the slot is one of `function_call_output`,
|
|
2507
|
-
/// `local_shell_call_output`, `apply_patch_call_output`. Used to
|
|
2508
|
-
/// gate the response-output floor.
|
|
2509
|
-
is_output_item: bool,
|
|
2510
|
-
}
|
|
2511
|
-
|
|
2512
|
-
/// Body view for the Responses request; accepts both `input` (canonical)
|
|
2513
|
-
/// and `messages` (alias).
|
|
2514
|
-
#[derive(Deserialize)]
|
|
2515
|
-
struct ResponsesBodyView<'a> {
|
|
2516
|
-
#[serde(borrow, default)]
|
|
2517
|
-
input: Option<Vec<&'a RawValue>>,
|
|
2518
|
-
#[serde(borrow, default)]
|
|
2519
|
-
messages: Option<Vec<&'a RawValue>>,
|
|
2520
|
-
}
|
|
2521
|
-
|
|
2522
|
-
impl<'a> ResponsesBodyView<'a> {
|
|
2523
|
-
fn items(&self) -> Option<&Vec<&'a RawValue>> {
|
|
2524
|
-
self.input.as_ref().or(self.messages.as_ref())
|
|
2525
|
-
}
|
|
2526
|
-
}
|
|
2527
|
-
|
|
2528
|
-
#[derive(Deserialize)]
|
|
2529
|
-
struct OutputItemView<'a> {
|
|
2530
|
-
#[serde(borrow, default)]
|
|
2531
|
-
output: Option<&'a RawValue>,
|
|
2532
|
-
}
|
|
2533
|
-
|
|
2534
|
-
#[derive(Deserialize)]
|
|
2535
|
-
struct MessageItemView<'a> {
|
|
2536
|
-
#[serde(borrow, default)]
|
|
2537
|
-
content: Option<&'a RawValue>,
|
|
2538
|
-
}
|
|
2539
|
-
|
|
2540
|
-
/// Plan a single replacement slot for a Responses item at index
|
|
2541
|
-
/// `item_idx`. Returns `Ok(None)` when the item exists but has no
|
|
2542
|
-
/// compressible payload (e.g. message with array content where every
|
|
2543
|
-
/// part is non-text).
|
|
2544
|
-
fn plan_responses_item(
|
|
2545
|
-
body_raw: &[u8],
|
|
2546
|
-
item_idx: usize,
|
|
2547
|
-
kind_tag: &str,
|
|
2548
|
-
) -> Result<Option<ResponsesPlanSlot>, PlanError> {
|
|
2549
|
-
let body_str = std::str::from_utf8(body_raw).map_err(|_| PlanError::ParseFailed)?;
|
|
2550
|
-
let body: ResponsesBodyView<'_> =
|
|
2551
|
-
serde_json::from_str(body_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2552
|
-
let items = body.items().ok_or(PlanError::ParseFailed)?;
|
|
2553
|
-
let item_raw = items.get(item_idx).ok_or(PlanError::TargetOutOfBounds)?;
|
|
2554
|
-
let item_offset_in_body =
|
|
2555
|
-
bytes_offset_of(body_str, item_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2556
|
-
|
|
2557
|
-
match kind_tag {
|
|
2558
|
-
"function_call_output" | "local_shell_call_output" | "apply_patch_call_output" => {
|
|
2559
|
-
let view: OutputItemView<'_> =
|
|
2560
|
-
serde_json::from_str(item_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2561
|
-
let Some(output_raw) = view.output else {
|
|
2562
|
-
return Ok(None);
|
|
2563
|
-
};
|
|
2564
|
-
let output_offset_in_item = bytes_offset_of(item_raw.get(), output_raw.get())
|
|
2565
|
-
.ok_or(PlanError::OffsetMissing)?;
|
|
2566
|
-
let output_offset_in_body = item_offset_in_body + output_offset_in_item;
|
|
2567
|
-
let output_str = output_raw.get();
|
|
2568
|
-
// `output` must be a JSON string for compression to apply.
|
|
2569
|
-
// Nested-object `output` (rare) falls through.
|
|
2570
|
-
if !output_str.starts_with('"') {
|
|
2571
|
-
return Ok(None);
|
|
2572
|
-
}
|
|
2573
|
-
let unescaped: String =
|
|
2574
|
-
serde_json::from_str(output_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2575
|
-
Ok(Some(ResponsesPlanSlot {
|
|
2576
|
-
block_index: None,
|
|
2577
|
-
block_type: kind_tag.to_string(),
|
|
2578
|
-
content_text: unescaped,
|
|
2579
|
-
content_byte_range: (
|
|
2580
|
-
output_offset_in_body,
|
|
2581
|
-
output_offset_in_body + output_str.len(),
|
|
2582
|
-
),
|
|
2583
|
-
is_output_item: true,
|
|
2584
|
-
}))
|
|
2585
|
-
}
|
|
2586
|
-
"message" => {
|
|
2587
|
-
let view: MessageItemView<'_> =
|
|
2588
|
-
serde_json::from_str(item_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2589
|
-
let Some(content_raw) = view.content else {
|
|
2590
|
-
return Ok(None);
|
|
2591
|
-
};
|
|
2592
|
-
let content_offset_in_item = bytes_offset_of(item_raw.get(), content_raw.get())
|
|
2593
|
-
.ok_or(PlanError::OffsetMissing)?;
|
|
2594
|
-
let content_offset_in_body = item_offset_in_body + content_offset_in_item;
|
|
2595
|
-
let content_str = content_raw.get();
|
|
2596
|
-
|
|
2597
|
-
// Case A: stringly-typed content.
|
|
2598
|
-
if content_str.starts_with('"') {
|
|
2599
|
-
let unescaped: String =
|
|
2600
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2601
|
-
return Ok(Some(ResponsesPlanSlot {
|
|
2602
|
-
block_index: None,
|
|
2603
|
-
block_type: "message_string".to_string(),
|
|
2604
|
-
content_text: unescaped,
|
|
2605
|
-
content_byte_range: (
|
|
2606
|
-
content_offset_in_body,
|
|
2607
|
-
content_offset_in_body + content_str.len(),
|
|
2608
|
-
),
|
|
2609
|
-
is_output_item: false,
|
|
2610
|
-
}));
|
|
2611
|
-
}
|
|
2612
|
-
|
|
2613
|
-
// Case B: array of typed content parts. The Responses
|
|
2614
|
-
// spec uses `{type: "input_text", text: "..."}` and
|
|
2615
|
-
// `{type: "output_text", text: "..."}`. Both are
|
|
2616
|
-
// compressible. Anything else (image, file, etc.) is
|
|
2617
|
-
// skipped.
|
|
2618
|
-
let parts: Vec<&RawValue> =
|
|
2619
|
-
serde_json::from_str(content_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2620
|
-
|
|
2621
|
-
// Pick the first text-shaped part for compression. The
|
|
2622
|
-
// common Codex shape has exactly one input_text per
|
|
2623
|
-
// user message; the assistant final-answer shape has
|
|
2624
|
-
// exactly one output_text. If a future shape carries
|
|
2625
|
-
// multiple, we compress the first only — the rest still
|
|
2626
|
-
// round-trip byte-equal because we never plan a second
|
|
2627
|
-
// slot.
|
|
2628
|
-
for (part_idx, part_raw) in parts.iter().enumerate() {
|
|
2629
|
-
let header: BlockHeader<'_> =
|
|
2630
|
-
serde_json::from_str(part_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2631
|
-
let block_type = header.r#type.unwrap_or("unknown");
|
|
2632
|
-
let is_text = block_type == "input_text"
|
|
2633
|
-
|| block_type == "output_text"
|
|
2634
|
-
|| block_type == "text";
|
|
2635
|
-
if !is_text {
|
|
2636
|
-
continue;
|
|
2637
|
-
}
|
|
2638
|
-
|
|
2639
|
-
#[derive(Deserialize)]
|
|
2640
|
-
struct TextHeader<'a> {
|
|
2641
|
-
#[serde(borrow, default)]
|
|
2642
|
-
text: Option<&'a RawValue>,
|
|
2643
|
-
}
|
|
2644
|
-
let h: TextHeader<'_> =
|
|
2645
|
-
serde_json::from_str(part_raw.get()).map_err(|_| PlanError::ParseFailed)?;
|
|
2646
|
-
let Some(text_raw) = h.text else { continue };
|
|
2647
|
-
|
|
2648
|
-
let part_offset_in_content =
|
|
2649
|
-
bytes_offset_of(content_str, part_raw.get()).ok_or(PlanError::OffsetMissing)?;
|
|
2650
|
-
let part_offset_in_body = content_offset_in_body + part_offset_in_content;
|
|
2651
|
-
let text_offset_in_part = bytes_offset_of(part_raw.get(), text_raw.get())
|
|
2652
|
-
.ok_or(PlanError::OffsetMissing)?;
|
|
2653
|
-
|
|
2654
|
-
let text_str = text_raw.get();
|
|
2655
|
-
if !text_str.starts_with('"') {
|
|
2656
|
-
continue;
|
|
2657
|
-
}
|
|
2658
|
-
let unescaped: String =
|
|
2659
|
-
serde_json::from_str(text_str).map_err(|_| PlanError::ParseFailed)?;
|
|
2660
|
-
|
|
2661
|
-
let text_start_in_body = part_offset_in_body + text_offset_in_part;
|
|
2662
|
-
let text_end_in_body = text_start_in_body + text_str.len();
|
|
2663
|
-
|
|
2664
|
-
return Ok(Some(ResponsesPlanSlot {
|
|
2665
|
-
block_index: Some(part_idx),
|
|
2666
|
-
block_type: format!("message_{block_type}"),
|
|
2667
|
-
content_text: unescaped,
|
|
2668
|
-
content_byte_range: (text_start_in_body, text_end_in_body),
|
|
2669
|
-
is_output_item: false,
|
|
2670
|
-
}));
|
|
2671
|
-
}
|
|
2672
|
-
Ok(None)
|
|
2673
|
-
}
|
|
2674
|
-
_ => Ok(None),
|
|
2675
|
-
}
|
|
2676
|
-
}
|
|
2677
|
-
|
|
2678
|
-
#[cfg(test)]
|
|
2679
|
-
mod openai_responses_tests {
|
|
2680
|
-
use super::*;
|
|
2681
|
-
use serde_json::json;
|
|
2682
|
-
|
|
2683
|
-
fn body(value: Value) -> Vec<u8> {
|
|
2684
|
-
serde_json::to_vec(&value).unwrap()
|
|
2685
|
-
}
|
|
2686
|
-
|
|
2687
|
-
#[test]
|
|
2688
|
-
fn empty_input_yields_no_change() {
|
|
2689
|
-
let b = body(json!({"model": "gpt-4o", "input": []}));
|
|
2690
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, DEFAULT_MODEL).unwrap();
|
|
2691
|
-
assert!(matches!(out, LiveZoneOutcome::NoChange { .. }));
|
|
2692
|
-
}
|
|
2693
|
-
|
|
2694
|
-
#[test]
|
|
2695
|
-
fn no_input_field_errors() {
|
|
2696
|
-
let b = body(json!({"model": "gpt-4o"}));
|
|
2697
|
-
let err =
|
|
2698
|
-
compress_openai_responses_live_zone(&b, AuthMode::Payg, DEFAULT_MODEL).unwrap_err();
|
|
2699
|
-
assert!(matches!(err, LiveZoneError::NoMessagesArray));
|
|
2700
|
-
}
|
|
2701
|
-
|
|
2702
|
-
#[test]
|
|
2703
|
-
fn invalid_json_errors() {
|
|
2704
|
-
let err = compress_openai_responses_live_zone(b"not json", AuthMode::Payg, DEFAULT_MODEL)
|
|
2705
|
-
.unwrap_err();
|
|
2706
|
-
assert!(matches!(err, LiveZoneError::BodyNotJson(_)));
|
|
2707
|
-
}
|
|
2708
|
-
|
|
2709
|
-
#[test]
|
|
2710
|
-
fn output_below_512b_skipped() {
|
|
2711
|
-
// 256 B output → below the output-item floor.
|
|
2712
|
-
let small = "x".repeat(256);
|
|
2713
|
-
let b = body(json!({
|
|
2714
|
-
"model": "gpt-4o",
|
|
2715
|
-
"input": [
|
|
2716
|
-
{"type": "function_call_output", "call_id": "c1", "output": small}
|
|
2717
|
-
]
|
|
2718
|
-
}));
|
|
2719
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2720
|
-
match &out {
|
|
2721
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2722
|
-
assert_eq!(manifest.block_outcomes.len(), 1);
|
|
2723
|
-
match &manifest.block_outcomes[0].action {
|
|
2724
|
-
BlockAction::BelowByteThreshold {
|
|
2725
|
-
content_type,
|
|
2726
|
-
byte_count,
|
|
2727
|
-
threshold_bytes,
|
|
2728
|
-
} => {
|
|
2729
|
-
assert_eq!(*content_type, "output_item");
|
|
2730
|
-
assert_eq!(*byte_count, 256);
|
|
2731
|
-
assert_eq!(*threshold_bytes, RESPONSES_OUTPUT_MIN_BYTES);
|
|
2732
|
-
}
|
|
2733
|
-
other => panic!("expected BelowByteThreshold, got {other:?}"),
|
|
2734
|
-
}
|
|
2735
|
-
}
|
|
2736
|
-
_ => panic!("expected NoChange"),
|
|
2737
|
-
}
|
|
2738
|
-
}
|
|
2739
|
-
|
|
2740
|
-
#[test]
|
|
2741
|
-
fn plans_all_same_frame_function_outputs() {
|
|
2742
|
-
// Codex can batch parallel tool results in a single
|
|
2743
|
-
// response.create frame. They are all current-frame live
|
|
2744
|
-
// inputs, so each byte-safe output string gets a slot.
|
|
2745
|
-
let b = body(json!({
|
|
2746
|
-
"model": "gpt-4o",
|
|
2747
|
-
"input": [
|
|
2748
|
-
{"type": "function_call_output", "call_id": "c1", "output": "early"},
|
|
2749
|
-
{"type": "function_call", "call_id": "c2", "name": "f", "arguments": "{}"},
|
|
2750
|
-
{"type": "function_call_output", "call_id": "c2", "output": "late"},
|
|
2751
|
-
]
|
|
2752
|
-
}));
|
|
2753
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2754
|
-
let manifest = match &out {
|
|
2755
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
2756
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
2757
|
-
};
|
|
2758
|
-
let outputs: Vec<_> = manifest
|
|
2759
|
-
.block_outcomes
|
|
2760
|
-
.iter()
|
|
2761
|
-
.filter(|b| b.block_type == "function_call_output")
|
|
2762
|
-
.collect();
|
|
2763
|
-
assert_eq!(outputs.len(), 2);
|
|
2764
|
-
assert_eq!(outputs[0].message_index, 0);
|
|
2765
|
-
assert_eq!(outputs[1].message_index, 2);
|
|
2766
|
-
}
|
|
2767
|
-
|
|
2768
|
-
#[test]
|
|
2769
|
-
fn compresses_multiple_same_frame_outputs() {
|
|
2770
|
-
let mut first = String::new();
|
|
2771
|
-
let mut second = String::new();
|
|
2772
|
-
for i in 0..400 {
|
|
2773
|
-
first.push_str(&format!(
|
|
2774
|
-
"./src/foo_{i}.rs:12: error[E0308]: mismatched types in module foo_{i}\n"
|
|
2775
|
-
));
|
|
2776
|
-
second.push_str(&format!(
|
|
2777
|
-
"./tests/bar_{i}.rs:44: warning: unused variable in test bar_{i}\n"
|
|
2778
|
-
));
|
|
2779
|
-
}
|
|
2780
|
-
let b = body(json!({
|
|
2781
|
-
"model": "gpt-4o",
|
|
2782
|
-
"input": [
|
|
2783
|
-
{"type": "function_call_output", "call_id": "c1", "output": first},
|
|
2784
|
-
{"type": "function_call", "call_id": "c2", "name": "f", "arguments": "{}"},
|
|
2785
|
-
{"type": "function_call_output", "call_id": "c2", "output": second},
|
|
2786
|
-
]
|
|
2787
|
-
}));
|
|
2788
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2789
|
-
let manifest = match &out {
|
|
2790
|
-
LiveZoneOutcome::NoChange { manifest } => manifest,
|
|
2791
|
-
LiveZoneOutcome::Modified { manifest, .. } => manifest,
|
|
2792
|
-
};
|
|
2793
|
-
let compressed_outputs = manifest
|
|
2794
|
-
.block_outcomes
|
|
2795
|
-
.iter()
|
|
2796
|
-
.filter(|b| {
|
|
2797
|
-
b.block_type == "function_call_output"
|
|
2798
|
-
&& matches!(b.action, BlockAction::Compressed { .. })
|
|
2799
|
-
})
|
|
2800
|
-
.count();
|
|
2801
|
-
assert_eq!(compressed_outputs, 2, "{manifest:?}");
|
|
2802
|
-
}
|
|
2803
|
-
|
|
2804
|
-
#[test]
|
|
2805
|
-
fn unknown_item_types_passthrough_no_slot() {
|
|
2806
|
-
// Items the dispatcher doesn't compress — no replacement.
|
|
2807
|
-
let b = body(json!({
|
|
2808
|
-
"model": "gpt-4o",
|
|
2809
|
-
"input": [
|
|
2810
|
-
{"type": "reasoning", "id": "r1", "encrypted_content": "opaque"},
|
|
2811
|
-
{"type": "compaction", "id": "k1", "encrypted_content": "opaque"},
|
|
2812
|
-
{"type": "future_item_v2", "novel": true},
|
|
2813
|
-
]
|
|
2814
|
-
}));
|
|
2815
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2816
|
-
match &out {
|
|
2817
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2818
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
2819
|
-
}
|
|
2820
|
-
_ => panic!("expected NoChange"),
|
|
2821
|
-
}
|
|
2822
|
-
}
|
|
2823
|
-
|
|
2824
|
-
#[test]
|
|
2825
|
-
fn large_log_output_compressed() {
|
|
2826
|
-
// Compressible build-output style log block with repeated
|
|
2827
|
-
// template lines. Above 2 KB so the output floor passes;
|
|
2828
|
-
// LogCompressor handles BuildOutput content type.
|
|
2829
|
-
let mut log = String::new();
|
|
2830
|
-
for i in 0..400 {
|
|
2831
|
-
log.push_str(&format!(
|
|
2832
|
-
"[2024-01-01 00:00:00] INFO compile.rs:42 building module foo_{i}\n"
|
|
2833
|
-
));
|
|
2834
|
-
}
|
|
2835
|
-
assert!(log.len() > 2048);
|
|
2836
|
-
let b = body(json!({
|
|
2837
|
-
"model": "gpt-4o",
|
|
2838
|
-
"input": [
|
|
2839
|
-
{"type": "local_shell_call_output", "call_id": "c1", "output": log}
|
|
2840
|
-
]
|
|
2841
|
-
}));
|
|
2842
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2843
|
-
match &out {
|
|
2844
|
-
LiveZoneOutcome::Modified { new_body, manifest } => {
|
|
2845
|
-
let new = new_body.get();
|
|
2846
|
-
assert!(new.len() < b.len());
|
|
2847
|
-
assert!(manifest
|
|
2848
|
-
.block_outcomes
|
|
2849
|
-
.iter()
|
|
2850
|
-
.any(|b| matches!(b.action, BlockAction::Compressed { .. })));
|
|
2851
|
-
}
|
|
2852
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2853
|
-
// RejectedNotSmaller is also an acceptable outcome
|
|
2854
|
-
// for the test fixture; what matters is that the
|
|
2855
|
-
// dispatcher *attempted* the compression.
|
|
2856
|
-
let attempted = manifest.block_outcomes.iter().any(|b| {
|
|
2857
|
-
matches!(
|
|
2858
|
-
b.action,
|
|
2859
|
-
BlockAction::Compressed { .. } | BlockAction::RejectedNotSmaller { .. }
|
|
2860
|
-
)
|
|
2861
|
-
});
|
|
2862
|
-
assert!(
|
|
2863
|
-
attempted,
|
|
2864
|
-
"expected dispatcher to attempt compression on a 2KB+ log fixture: {manifest:?}"
|
|
2865
|
-
);
|
|
2866
|
-
}
|
|
2867
|
-
}
|
|
2868
|
-
}
|
|
2869
|
-
|
|
2870
|
-
#[test]
|
|
2871
|
-
fn message_user_content_not_in_live_zone() {
|
|
2872
|
-
let b = body(json!({
|
|
2873
|
-
"model": "gpt-4o",
|
|
2874
|
-
"input": [
|
|
2875
|
-
{"type": "message", "role": "user",
|
|
2876
|
-
"content": [{"type": "input_text", "text": "describe this"}]}
|
|
2877
|
-
]
|
|
2878
|
-
}));
|
|
2879
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2880
|
-
match &out {
|
|
2881
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2882
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
2883
|
-
}
|
|
2884
|
-
_ => panic!("expected NoChange"),
|
|
2885
|
-
}
|
|
2886
|
-
}
|
|
2887
|
-
|
|
2888
|
-
#[test]
|
|
2889
|
-
fn headroom_retrieve_output_not_in_live_zone() {
|
|
2890
|
-
let retrieved = "retrieved original content ".repeat(100);
|
|
2891
|
-
let b = body(json!({
|
|
2892
|
-
"model": "gpt-4o",
|
|
2893
|
-
"input": [
|
|
2894
|
-
{
|
|
2895
|
-
"type": "function_call",
|
|
2896
|
-
"call_id": "call_retrieve",
|
|
2897
|
-
"name": "mcp__headroom__headroom_retrieve",
|
|
2898
|
-
"arguments": "{}"
|
|
2899
|
-
},
|
|
2900
|
-
{
|
|
2901
|
-
"type": "function_call_output",
|
|
2902
|
-
"call_id": "call_retrieve",
|
|
2903
|
-
"output": retrieved
|
|
2904
|
-
}
|
|
2905
|
-
]
|
|
2906
|
-
}));
|
|
2907
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2908
|
-
match &out {
|
|
2909
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2910
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
2911
|
-
}
|
|
2912
|
-
_ => panic!("expected NoChange"),
|
|
2913
|
-
}
|
|
2914
|
-
}
|
|
2915
|
-
|
|
2916
|
-
#[test]
|
|
2917
|
-
fn assistant_message_not_in_live_zone() {
|
|
2918
|
-
// Only user messages are eligible. An assistant `message`
|
|
2919
|
-
// item is never planned.
|
|
2920
|
-
let b = body(json!({
|
|
2921
|
-
"model": "gpt-4o",
|
|
2922
|
-
"input": [
|
|
2923
|
-
{"type": "message", "role": "assistant",
|
|
2924
|
-
"content": [{"type": "output_text", "text": "answer"}]}
|
|
2925
|
-
]
|
|
2926
|
-
}));
|
|
2927
|
-
let out = compress_openai_responses_live_zone(&b, AuthMode::Payg, "gpt-4o").unwrap();
|
|
2928
|
-
match &out {
|
|
2929
|
-
LiveZoneOutcome::NoChange { manifest } => {
|
|
2930
|
-
assert!(manifest.block_outcomes.is_empty());
|
|
2931
|
-
}
|
|
2932
|
-
_ => panic!("expected NoChange"),
|
|
2933
|
-
}
|
|
2934
|
-
}
|
|
2935
|
-
|
|
2936
|
-
#[test]
|
|
2937
|
-
fn no_change_reason_empty_input_is_no_eligible_items() {
|
|
2938
|
-
let manifest = CompressionManifest::empty();
|
|
2939
|
-
assert_eq!(
|
|
2940
|
-
summarize_openai_responses_no_change_reason(&manifest),
|
|
2941
|
-
"no_eligible_items"
|
|
2942
|
-
);
|
|
2943
|
-
}
|
|
2944
|
-
|
|
2945
|
-
#[test]
|
|
2946
|
-
fn no_change_reason_prefers_output_floor() {
|
|
2947
|
-
let manifest = CompressionManifest {
|
|
2948
|
-
messages_total: 1,
|
|
2949
|
-
messages_below_frozen_floor: 0,
|
|
2950
|
-
latest_user_message_index: Some(0),
|
|
2951
|
-
block_outcomes: vec![BlockOutcome {
|
|
2952
|
-
message_index: 0,
|
|
2953
|
-
block_index: None,
|
|
2954
|
-
block_type: "function_call_output".to_string(),
|
|
2955
|
-
action: BlockAction::BelowByteThreshold {
|
|
2956
|
-
content_type: "output_item",
|
|
2957
|
-
byte_count: 1024,
|
|
2958
|
-
threshold_bytes: RESPONSES_OUTPUT_MIN_BYTES,
|
|
2959
|
-
},
|
|
2960
|
-
}],
|
|
2961
|
-
};
|
|
2962
|
-
assert_eq!(
|
|
2963
|
-
summarize_openai_responses_no_change_reason(&manifest),
|
|
2964
|
-
"below_output_floor"
|
|
2965
|
-
);
|
|
2966
|
-
}
|
|
2967
|
-
}
|