Skip to content

Fix LLM filter cache key inputs#831

Draft
Colin-XKL wants to merge 1 commit into
devfrom
cursor/craft-cache-key-40d7
Draft

Fix LLM filter cache key inputs#831
Colin-XKL wants to merge 1 commit into
devfrom
cursor/craft-cache-key-40d7

Conversation

@Colin-XKL

@Colin-XKL Colin-XKL commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • Include the rendered llm-filter prompt and exact LLM payload in cache key generation.
  • Add a regression test that pre-seeds the expected cache key and verifies the processor does not call the LLM.

Testing

  • go test ./internal/craft -run TestLLMFilterProcessor_CacheKeyUsesFullPromptAndLLMPayload -count=1
  • go test ./internal/craft -count=1
  • task fix
  • go test ./...
  • task backend-build
  • task frontend-build
Open in Web Open in Cursor 

Summary by Sourcery

Improve LLM filter caching by keying on the full rendered prompt and article payload to prevent unnecessary LLM calls.

Bug Fixes:

  • Ensure LLM filter cache keys incorporate both the rendered prompt and full LLM article payload so cached results are correctly reused.

Enhancements:

  • Refactor generic condition prompt construction into a reusable helper used by both cache key generation and LLM invocation.

Tests:

  • Add a regression test verifying the LLM filter reuses cache when the full prompt and payload match, avoiding any LLM invocation.

Co-authored-by: Colin <Colin_XKL@outlook.com>
@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
feed-craft-admin Ready Ready Preview, Comment Jun 29, 2026 7:02am
feed-craft-doc Ready Ready Preview, Comment Jun 29, 2026 7:02am

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the LLM filter processor to generate cache keys based on the full prompt and the actual LLM payload rather than just the raw condition. It extracts the prompt construction logic into a helper function and adds a unit test to verify the new caching behavior. A review comment identifies a potential nil pointer dereference in the new cache key generator if the article is nil.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +344 to +348
return func(article *model.CraftArticle) (string, error) {
payload := ""
if payloadBuilder != nil {
payload = payloadBuilder(article)
}

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.

high

The payloadBuilder function is called with article without verifying if article is nil. If article is nil, accessing article.Title inside the payload builder (as done in newLLMFilterProcessor) will cause a nil pointer dereference panic. Adding a defensive check for nil at the beginning of the generator function prevents this potential panic.

	return func(article *model.CraftArticle) (string, error) {
		if article == nil {
			return "", fmt.Errorf("article is nil")
		}
		payload := ""
		if payloadBuilder != nil {
			payload = payloadBuilder(article)
		}

@sourcery-ai

sourcery-ai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Updates the LLM filter processor to generate cache keys from the fully rendered generic prompt plus the exact LLM article payload, and adds a regression test to ensure cached responses prevent redundant LLM calls.

Flow diagram for updated LLM filter cache key and call path

flowchart TD
    subgraph PromptPipeline
        C[condition or userPrompt] --> G[buildGenericConditionPrompt]
        G --> FP[full generic condition prompt]
        FP --> PH[util.GetTextContentHash prompt]
    end

    subgraph PayloadPipeline
        A[CraftArticle] --> P[BuildLLMArticlePayload]
        P --> PL[LLM article payload]
        PL --> LH[util.GetTextContentHash payload]
    end

    PH --> J[strings.Join promptHash or payloadHash]
    LH --> J
    J --> KH[util.GetTextContentHash cacheKey]

    KH --> M[GetCommonCachedArticlePredicate]
    M --> D{cache entry exists}
    D -->|yes| R[cached predicate result]
    D -->|no| L[CheckConditionWithLLM]
    L --> R
Loading

File-Level Changes

Change Details Files
LLM filter processor now uses a cache key based on the full rendered generic prompt and LLM article payload instead of just the condition text and article content.
  • Capture the rendered generic condition prompt in newLLMFilterProcessor and reuse it for both cache key generation and LLM calls
  • Replace newArticleTitleContentCacheKeyGenerator usage with a newArticleLLMPayloadCacheKeyGenerator that hashes the rendered prompt and built LLM payload
  • Switch the filter processor predicate to call CheckConditionWithLLM with the prebuilt full prompt instead of CheckConditionWithGenericPrompt
internal/craft/llm_processors.go
Introduced a dedicated helper to build the generic condition prompt and reused it in the condition-checking helper.
  • Refactor CheckConditionWithGenericPrompt to delegate prompt construction to buildGenericConditionPrompt
  • Add buildGenericConditionPrompt helper that returns the multi-line evaluation prompt string
  • Simplify CheckConditionWithGenericPrompt to just call CheckConditionWithLLM with the constructed full prompt
internal/craft/common_llm_logic.go
Added a regression test that verifies the cache key uses the full prompt and LLM payload by pre-seeding Redis and asserting no LLM calls occur.
  • Stub llmContextCaller to fail the test if invoked, ensuring cache hit behavior is required
  • Construct the exact full generic prompt and LLM article payload for a test article and derive the expected hash used in the cache key
  • Pre-seed Redis with the computed cache key and validate that processing removes the article without invoking the LLM
internal/craft/runtime_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

2 participants