← Back to Pull requests

fix(tasks): cap tasks retained per caller and evict finished ones first #289

Closed
unknown closed this pull request without merging on Oct 14, 2026 11:06 UTC
View on GitHub
unknown unknown commented on Oct 13, 2026 11:21 UTC
Author

Problem

MemoryTaskStore only refused new tasks when the whole store (1000 records) was full, and finished tasks stayed until their ttl expired. One caller creating fast tasks with a long ttl could fill the store, after which every other caller's task creation failed for up to an hour with a bare -32603 "Internal server error". RedisTaskStore had no cap at all.

Fix

  • New option taskMaxRetainedPerPrincipal (default 100): the most tasks one principal may keep stored, finished ones included. Without authorization all callers share one quota.

  • When a caller is at its quota, its oldest finished (completed/failed/cancelled) task is evicted. The call is refused only if all of its tasks are still running.

  • The memory store's global cap now evicts the caller's oldest finished task, then the oldest finished task of any caller, before refusing.

  • RedisTaskStore enforces the quota atomically in a Lua script with a per-owner sorted set (mcp:tasks:owner:<subject> / mcp:tasks:anonymous, scored by creation time). The script prunes ids whose task key has expired, evicts as needed, and stretches the index expiry so it outlives its tasks.

  • A refused creation returns -32029 "Task limit reached". Stores throw TaskLimitError, which is exported along with TASK_LIMIT_REACHED so custom stores can use it.

  • MemoryTaskStore now also accepts { maxTasks, maxTasksPerPrincipal }. The old numeric argument still works.
  • Relation to #239

    #239 (merged) added per-owner Redis sorted sets for tasks/list pagination. This PR now shares that index: #239's INDEX_ADD_SCRIPT is folded into CREATE_SCRIPT, which prunes expired ids, evicts, stores the task and adds it to the same per-owner set (same key, score and member, same index-expiry rule) in one atomic step. There is no global mcp:tasks index.

    Breaking change

    Strictly speaking, yes. A single caller (or all callers together when authorization is off) can now keep at most 100 tasks by default. Older finished tasks beyond that are evicted before their ttl expires, and a 101st concurrently running task is refused. Raise taskMaxRetainedPerPrincipal if you need more.

    Fixes #249

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01UF4S3w7iA83V9DVsasLJzM

    Diff: 288 lines (+216 / -72) across 3 files Reviewer: rozzilla Review duration: 1425m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 288 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.339
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #289"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.8% (MEDIUM) · Higher = larger blast radius
    39.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.7% · Safe to merge
    15.7%