Skip to content

bug: server/server.js uses node-cache with a fixed 10-minute TTL for all AI responses β€” identical prompts from different users with different code contexts get the same cached responseΒ #887

Description

@prince-pokharna

πŸ› Problem Statement

The README documents: "AI Features β€” Groq SDK β€” llama-3.3-70b-versatile + node-cache." In server/server.js, AI responses are cached using node-cache. The cache key is almost certainly derived only from the prompt text (the AI mode + language + request type).

This creates a critical correctness bug: the user's actual code is not part of the cache key.

When User A asks "Explain this code" on their Python bubble sort implementation, the response is cached. When User B asks "Explain this code" on their JavaScript binary search 10 minutes later, if the cache key doesn't include the code, User B receives User A's explanation of bubble sort β€” completely wrong, and potentially confusing enough to cause real debugging harm.

Additionally, a user who fixes their code and asks "Fix this" again within 10 minutes gets the same fix suggestion for their old, already-fixed code.

Root Cause

// Likely current cache key (server/routes/ai.js or server/server.js)
const cacheKey = `${aiMode}_${language}`;  // ← Missing the code content

// Should be:
const cacheKey = crypto
  .createHash('sha256')
  .update(`${aiMode}_${language}_${userCode}_${stdin}`)
  .digest('hex');

Proposed Fix

Include a hash of the actual code content in the cache key. Use SHA-256 (no security requirement, just uniqueness):

// server/routes/ai.js or server/services/aiService.js
const crypto = require('crypto');

function buildCacheKey(mode, language, code, stdin = '') {
  const contentHash = crypto
    .createHash('sha256')
    .update(`${code}${stdin}`)
    .digest('hex')
    .slice(0, 16); // First 16 chars is sufficient for uniqueness

  return `${mode}_${language}_${contentHash}`;
}

Also reduce the TTL for "Fix" and "Explain" modes to 5 minutes (code changes frequently) while keeping the longer TTL for "Generate Tests" (test generation is less code-sensitive).

Files to Modify

File Change
server/routes/ai.js (or equivalent) Update buildCacheKey() to include code hash

Suggested labels: bug, backend, performance, level: beginner

I would like to work on this. Could you please assign it to me?

Metadata

Metadata

Assignees

No one assigned

    Labels

    gssocOfficial GSSoC '26 issue tagtype:bugVulnerability or logical bug fixestype:designTactile visual design and UI alignmentstype:docsDocumentation and guide upgradestype:featureNew functional feature additionstype:performancePerformance and latency optimizationstype:securitySecurity patches and threat fixestype:testingIntegration and unit test coverage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions