Skip to content

fix(harness): preserve explicit prompt caching breakpoints past trailing projections - #4555

Open
Acerola-1 wants to merge 4 commits into
agentscope-ai:mainfrom
Acerola-1:fix/harness-explicit-cache-control
Open

Acerola-1 wants to merge 4 commits into
agentscope-ai:mainfrom
Acerola-1:fix/harness-explicit-cache-control

Conversation

@Acerola-1

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT

Description

Refs #4538 (PR 3, explicit cache_control breakpoint skip for Harness tail projections and Anthropic #2878 alignment).

Problem

When GenerateOptions.builder().cacheControl(true).build() is enabled on a HarnessAgent:

  1. HarnessContextBuilder.layout() appends request-only trailing projections after durable conversation history (such as task_delivery, todo_state, plan_state, harness:session-state, tail workspace_materials, and an end-moved skill_catalog when guarding against a trailing ASSISTANT prefill).
  2. DashScope and OpenAI-compatible formatters only evaluated lastIndex in applyAutomaticCacheControl(...) and did not scan backward past MessageMetadataKeys.CACHE_CONTROL = false or empty/non-cacheable messages (such as tool-call-only assistant messages).
  3. Anthropic had not yet adopted the request-scoped doFormat(List<Msg>, GenerateOptions) / MessageMetadataKeys.CACHE_CONTROL contract from fix(model): move cache control to content blocks #2878 and unconditionally marked the final formatted MessageParam.

As a result, each turn's automatic conversation breakpoint landed on transient tail projections rather than the last durable message, preventing cached history prefixes from being reused on subsequent turns.

Summary of Changes

  • Harness tail projection marking (HarnessContextBuilder, io.agentscope.core.internal.PromptCacheMetadata):
    • Mark request-only projections placed after durable history (including a skill catalog moved to the end on trailing-ASSISTANT fallback) on the request copy with MessageMetadataKeys.CACHE_CONTROL = false (preserving any caller-supplied directive) and internal _cache_control_projection = true.
    • Keep durable canonical history and leading stable projections untouched, preserve layout() idempotency, and leave wire output unchanged when GenerateOptions.cacheControl is disabled.
  • DashScope & OpenAI-compatible formatters (DashScopeChatFormatter, DashScopeMultiAgentFormatter, OpenAIBaseFormatter, OpenAIMultiAgentFormatter):
    • Scan backward in applyAutomaticCacheControl(...) past excluded (false), empty-text (""), or non-cacheable messages so the automatic breakpoint lands on the latest cacheable durable message while stopping at an eligible explicit true marker.
    • Preserve the multi-agent grouping boundary between durable conversation history and trailing projections when conversation-level prompt caching is active (while keeping grouping unchanged when caching is off).
  • Anthropic fix(model): move cache control to content blocks #2878 alignment & breakpoint budget (AnthropicChatModel, AnthropicBaseFormatter, AnthropicChatFormatter, AnthropicMultiAgentFormatter, AnthropicMessageConverter, AnthropicToolsHelper, AnthropicFormatterCacheHooks):
    • Implement request-scoped doFormat(List<Msg>, GenerateOptions) honoring MessageMetadataKeys.CACHE_CONTROL (true/false) on the leading SYSTEM prompt and conversation messages (including merged tool results, split assistant tool-result messages, thinking-only/empty-text messages, and multi-agent groups).
    • Account for all non-message breakpoints (client tools, server tools, system blocks, and valid top-level additionalBodyParams.cache_control), revoke framework-managed automatic system then client tool markers when explicit markers exceed Anthropic's 4-breakpoint limit, and log a warning while forwarding supplied markers unchanged if supplied markers themselves exceed 4.
    • Preserve backward compatibility for custom subclasses overriding legacy format(List) / doFormat(List) / applyCacheControl(List) hooks via AnthropicFormatterCacheHooks, while letting benign subclasses and Mockito spies inherit request-scoped caching.
  • Tests & Documentation:
    • Add and expand unit/regression tests in HarnessContextBuilderTest, ContextPipelineV2Test, DashScopeCacheControlTest, OpenAICacheControlTest, AnthropicCacheControlTest, and AnthropicChatModelCacheControlTest.
    • Synchronize English and Chinese docs in docs/dev/{en,zh}/docs/harness/context.md and docs/dev/{en,zh}/integration/model/anthropic.md.

Note: verification used unit/mock-server/formatter tests and offline Harness fixtures; live provider-side prompt-cache hit rates were not measured.

Validation

  • Full mvn -B -T1C clean verify passed across all 114 modules on d22a9bc4a (prior to the final comment-only English translation commit).
  • Docs checks (npm test 20/20, npm run validate 660 pages / 1,011 redirects, npm run broken-links 0 broken links) passed on d22a9bc4a.
  • After the final comment-only commit (9843e1421):
    • mvn -T1C -pl agentscope-core,agentscope-harness,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic spotless:check passed.
    • Targeted tests (AnthropicCacheControlTest, AnthropicChatModelCacheControlTest, HarnessContextBuilderTest, ContextPipelineV2Test) passed (agentscope-extensions-model-anthropic: 45 run, 0 failures/errors/skipped; agentscope-harness: 38 run, 0 failures/errors/skipped).

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@mintlify

mintlify Bot commented Dec 2, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 9843e14.

Approve preview

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant