Skip to content

fix: harden multi-tenant resource management and quota enforcement#656

Merged
njbrake merged 2 commits intomainfrom
fix/multi-tenant-pipeline-and-user-isolation
Mar 18, 2026
Merged

fix: harden multi-tenant resource management and quota enforcement#656
njbrake merged 2 commits intomainfrom
fix/multi-tenant-pipeline-and-user-isolation

Conversation

@njbrake
Copy link
Collaborator

@njbrake njbrake commented Mar 18, 2026

Description

Harden OSS layer for multi-tenant SaaS usage by premium:

  • LRU-bounded store caches: Replace unbounded dict caches in SessionStore and MemoryStore with OrderedDict-based LRU (cap: 256 entries). Prevents memory leaks when many tenants are active.
  • Token accumulation: Accumulate input_tokens/output_tokens across multi-round agent loops in AgentResponse, enabling premium to enforce per-user token quotas.
  • Pluggable estimate quota hooks: Add set_estimate_quota_hooks(check, increment) API so premium can inject quota enforcement at the tool level without modifying OSS code.

Companion PR: mozilla-ai/clawbolt-premium (fix/wire-premium-pipeline-and-ci)

Type

  • Feature
  • Bug fix
  • Refactor
  • Test
  • CI/CD
  • Documentation

Checklist

  • Tests pass (uv run pytest -v)
  • Lint passes (ruff check backend/ && ruff format --check backend/)
  • New tests added for new functionality
  • Bug fixes include regression tests

AI Usage

  • AI-assisted (architectural review and implementation by Claude)
  • No AI used

🤖 Generated with Claude Code

njbrake and others added 2 commits March 17, 2026 21:12
…lti-tenant

Two fixes to support multi-tenant (premium) mode:

1. Pipeline override: add set_pipeline_override() and get_active_pipeline()
   to router.py so the premium plugin can inject quota-check and
   usage-tracking steps into the agent pipeline. Previously,
   handle_inbound_message() always used DEFAULT_PIPELINE, bypassing
   all premium quota enforcement.

2. Singleton user reuse guard: in _get_or_create_user(), skip the
   "reuse sole existing user" path when settings.premium_plugin is set.
   Without this guard, the first new Telegram sender in a multi-tenant
   deployment would have their messages linked to an existing user's
   account (data leak).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Bound per-user store caches (SessionStore, MemoryStore) with LRU eviction
  to prevent unbounded memory growth under multi-tenant load
- Accumulate input/output token counts across multi-round agent loops in
  AgentResponse so premium can enforce per-user token quotas
- Add pluggable estimate quota hooks (check + increment) so premium can
  inject quota enforcement without modifying OSS code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@njbrake njbrake merged commit 4618a14 into main Mar 18, 2026
9 checks passed
@njbrake njbrake deleted the fix/multi-tenant-pipeline-and-user-isolation branch March 18, 2026 02:04
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