Visitar URL original
feat(core): add prompt cache rules by hiroshi1000 · Pull Request #53927 · anomalyco/opencode · GitHub
Skip to content

feat(core): add prompt cache rules - #53927

Open
hiroshi1000 wants to merge 8 commits into
anomalyco:devfrom
hiroshi1000:cache-rules
Open

hiroshi1000 wants to merge 8 commits into
anomalyco:devfrom
hiroshi1000:cache-rules

Conversation

@hiroshi1000

@hiroshi1000 hiroshi1000 commented Oct 8, 2026 •

Copy link
Copy Markdown

Issue for this PR

Refs #51109 and the configuration proposal.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Add a top-level cache map keyed by configured provider ID. Rules match the actual agent, model, and subagent context; the most specific containing rule wins independently of order. Ambiguous and duplicate rules are diagnosed at configuration load; only the affected provider’s array is disabled, preserving unrelated configuration and preventing lower-priority rules from reappearing. Higher-priority config files replace each provider's whole rule array.

Apply native Anthropic cache_control and OpenAI prompt_cache_retention / prompt_cache_options settings, including subagents, title generation, and compaction. Unmatched requests and empty options keep existing behavior. Route-dependent unsupported settings return typed request errors. Cache controls reuse existing route policies, including Anthropic-compatible endpoints, Anthropic through OpenRouter, and supported Bedrock Converse models; unsupported Bedrock TTLs are rejected rather than shortened. Examples and limits are documented in specs/v2/prompt-cache.md.

GPT-5.6 and later accept native prompt_cache_options (ttl: "30m", mode: "implicit" | "explicit"), including compaction. Model and account compatibility are left to the API rather than inferred from model-ID regular expressions. The deprecated 24h retention may coexist with the new policy; it is not translated into a TTL. These rules do not insert explicit OpenAI breakpoints.

This is an independent implementation on v2, not a branch of #51110.

How did you verify your code works?

  • bun run check (repository lint and typechecks).
  • After the review fixes: 290 Core tests covering configuration, request preparation, title generation, and compaction; 572 AI tests covering native request bodies and existing provider/cache-policy behavior. Tests ran from their package directories. The initial implementation also passed 21 Schema tests.
  • Focused regression tests cover rule ordering, load-time conflicts, malformed cache isolation, config replacement/reload, custom provider IDs, subagents versus forks, typed failures, and provider-specific TTL handling. Replaced broad cache cross-product tests with named behavior cases.
  • Regenerated the client with bun run generate; a second generation produced identical output.

Screenshots / recordings

Not applicable; no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the new tests in packages/ai, packages/schema and packages/core (all pass) and type-checked those packages. I did not exercise this end to end against real providers. The rule-selection and body-lowering code looks careful, but a few issues should be addressed first:

  • A typo in cache drops the entire config file. I checked with ConfigNormalize.normalize: a misspelled condition (subAgent) makes the whole document rejected. The user's permissions, agents and providers silently disappear, with only a log warning. Other malformed keys are skipped individually.
  • Config mistakes crash requests. Ambiguous or unsupported rules are found only at request time and become defects (a plain throw and Effect.orDie) on every request, including title generation and compaction. These should be validated and reported when the config loads.
  • Route and model gating is narrow and hard-coded. cache_control is accepted only on three routes. Bedrock Converse, Anthropic-compatible providers and OpenRouter (for Anthropic models) already honour TTL through the existing cache policy. The OpenAI checks guess support from model IDs with regexes, which goes against the forward-compatibility guidance in packages/ai/AGENTS.md.
  • Design. The config exposes each provider's raw field names (cache_control, prompt_cache_retention) and then translates Anthropic's back into the existing cache: { ttlSeconds } policy. It would be good to get a maintainer's view on this shape in #51109 before going further.
  • Tests. The new session test in session-model-request-hooks.test.ts loops over every request kind × 5 scenarios × 2 providers in one large test, and the GPT body test loops over 6 model IDs × 4 option sets. Please cut these down to a few clear cases: one per behaviour (most specific rule wins, subagent vs fork, an OpenAI option reaching the body, reload).

Comment thread packages/core/src/config/normalize.ts Outdated
Comment thread packages/core/src/session/model-request.ts Outdated
Comment thread packages/ai/src/prompt-cache.ts Outdated
@hiroshi1000

Copy link
Copy Markdown
Author

Follow-up to the review summary, after 556825f:

  • Tests: Replaced the large request-kind/scenario/provider cross-product with named, focused cases in session-model-request-cache.test.ts. Restored the hooks test file to its hook-specific coverage. The cache tests now cover selection, subagent versus fork, reload, native OpenAI fields reaching request bodies, and typed failures separately; removed the GPT model-ID/option matrix.
  • Design: Kept the proposed provider-native configuration names in this revision. They distinguish the providers’ different cache controls without translating OpenAI retention into an Anthropic TTL. The AI layer reuses the existing internal cache policy for placement. The proposal remains in #51109 for maintainer feedback; this implementation does not imply maintainer approval of the configuration shape.

The three inline findings have individual replies with the corresponding changes. Validation: 290 Core tests, 572 AI tests, and the repository-wide bun run check passed. These checks cover local/recorded request handling, not live-provider end-to-end validation.

@thdxr
thdxr changed the base branch from v2 to dev October 10, 2026 19:36

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