Skip to content

feat(prompts): expose prompt cache operations#89

Merged
kxzk merged 7 commits into
mainfrom
feature/prompt-cache-public-operations
May 5, 2026
Merged

feat(prompts): expose prompt cache operations#89
kxzk merged 7 commits into
mainfrom
feature/prompt-cache-public-operations

Conversation

@kxzk
Copy link
Copy Markdown
Collaborator

@kxzk kxzk commented May 3, 2026

TL;DR

Expose flat prompt cache inspection, refresh, invalidation, and fetch metadata APIs.

Why

Prompt consumers need a supported way to inspect cache hit or miss source, bypass or refresh prompt fetches, and invalidate cache entries without relying on private cache internals.

Checklist

  • Has label
  • Has linked issue
  • Tests added for new behavior
  • Docs updated (if user-facing)

Linear: AAI-106

Summary

  • Added PromptFetchResult / PromptCacheKey and flat client APIs for cache metadata, refresh, invalidation, stats, and key inspection.
  • Added per-call cache_ttl, cache_ttl: 0 bypass, mutation invalidation, observer / ActiveSupport cache events, and cache_backend = :auto.
  • Updated memory and Rails cache adapters to support generation-based invalidation without prefix scans.

Verification

  • RBENV_VERSION=3.2.0 rbenv exec bundle _2.4.7_ exec rspec
  • RBENV_VERSION=3.2.0 rbenv exec bundle _2.4.7_ exec rubocop --format simple
  • RBENV_VERSION=3.2.0 rbenv exec bundle _2.4.7_ exec ruby scratchpad/aai_106_prompt_cache_validate.rb
  • npx --yes langfuse-cli api prompts get <validated prompt>
  • npx --yes langfuse-cli api prompts list --limit 10
  • npx --yes langfuse-cli api prompts delete <validated prompt>

Copilot AI review requested due to automatic review settings May 3, 2026 23:08
@kxzk kxzk added the enhancement New feature or request label May 3, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds public, flat prompt-cache APIs and fetch metadata so SDK consumers can inspect cache keys/status, refresh prompts, and perform invalidation without relying on private cache internals. It also introduces generation-based invalidation for both in-memory and Rails cache backends, plus per-call TTL overrides and event hooks.

Changes:

  • Introduce PromptFetchResult / PromptCacheKey and add Client#get_prompt_result plus flat cache operations (refresh/invalidate/clear/stats/key inspection/validation).
  • Add generation-based storage keys and name/global logical invalidation across memory + Rails cache adapters (avoids prefix scans).
  • Add per-call cache_ttl (including 0 bypass) and prompt cache events via observer + ActiveSupport instrumentation; add cache_backend = :auto.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
spec/langfuse/prompt_cache_spec.rb Adds coverage for generation-based storage keys, logical invalidation, and delete semantics.
spec/langfuse/config_spec.rb Adds validation tests for cache_backend: :auto and callable prompt_cache_observer.
spec/langfuse/client_spec.rb Adds tests for :auto backend selection and client-side fetch metadata / cache inspection APIs.
spec/langfuse/api_client_spec.rb Adds tests for API-client-level cache operations (metadata, bypass, refresh, invalidation, events).
lib/langfuse/stale_while_revalidate.rb Extends SWR to support TTL overrides, async refresh scheduling, and write helpers.
lib/langfuse/rails_cache_adapter.rb Adds generation-based invalidation + key generation, stats, delete/entry helpers, TTL override support.
lib/langfuse/prompt_fetch_result.rb Introduces public result/key objects for prompt fetch metadata and cache key inspection.
lib/langfuse/prompt_cache.rb Adds generation-based keying/invalidation, stats, delete/entry helpers, TTL override support.
lib/langfuse/config.rb Adds :auto backend + prompt_cache_observer configuration and validation.
lib/langfuse/client.rb Exposes flat client cache APIs and get_prompt_result; wires observer into ApiClient and supports per-call cache_ttl.
lib/langfuse/api_client.rb Implements prompt fetch metadata, cache operations, mutation invalidation, and cache event instrumentation.
lib/langfuse.rb Requires new public prompt fetch result types.
docs/PROMPTS.md Documents per-call cache TTL/bypass and operational visibility APIs.
docs/CONFIGURATION.md Documents cache_backend: :auto and prompt_cache_observer.
docs/CACHING.md Adds “Public Cache Operations” section, cache key semantics, invalidation model, and events.
docs/API_REFERENCE.md Updates signatures and documents get_prompt_result + flat cache operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/langfuse/stale_while_revalidate.rb
Comment thread lib/langfuse/client.rb
Comment thread lib/langfuse/stale_while_revalidate.rb
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 3, 2026

kxzk added 6 commits May 4, 2026 03:36
- Centralize label-defaulting on PromptCacheKey#resolved_label
- Share event payload builder between ApiClient and Client
- Memoize cache backend name and ActiveSupport::Notifications lookup
- Skip event emission early when no observers are configured
- Drop dead to_sym coercions and uncalled fetch_with_simple_cache helpers
@kxzk kxzk merged commit 3c6318e into main May 5, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants