Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(ai): enforce assistant enable state and conversation cost limits

Juan Denis54028fbff4ac
diff --git a/backend/app/api/ai.py b/backend/app/api/ai.py
index a8ccddaf..49c26b10 100644
--- a/backend/app/api/ai.py
+++ b/backend/app/api/ai.py
@@ -183,6 +183,15 @@ def update_settings():
user = get_current_user()
uid = user.id if user else None
+ if 'max_cost_usd' in data:
+ import math
+ try:
+ cost = float(data['max_cost_usd'])
+ except (TypeError, ValueError):
+ raise ValidationError('Cost limit must be a finite number of zero or more.')
+ if isinstance(data['max_cost_usd'], bool) or not math.isfinite(cost) or cost < 0:
+ raise ValidationError('Cost limit must be a finite number of zero or more.')
+
field_map = {
'enabled': ('ai_enabled', bool),
'provider': ('ai_provider', str),
@@ -391,6 +400,8 @@ def delete_conversation(conversation_id):
@ai_bp.route('/chat', methods=['POST'])
@jwt_required()
def chat():
+ if not ai_service.is_enabled():
+ return jsonify({'error': 'AI assistant is disabled. Enable it in Settings > AI Assistant.'}), 503
ai_service.ensure_initialized()
user = get_current_user()
if not ai_service.is_configured():
@@ -433,6 +444,7 @@ def chat():
def _run_chat(row, user, mode, page_context, attachment_refs, message, safe_message):
attachment_result = resolve_attachments(user, attachment_refs)
_persist_user_message(row, message, attachments=attachment_result['manifest'])
+ conv = None
try:
# gate=None: write tools refuse (no interactive confirmation in this mode).
conv = ai_service.build_conversation(
@@ -443,6 +455,12 @@ def _run_chat(row, user, mode, page_context, attachment_refs, message, safe_mess
except ai_service.AIProtectionError as exc:
raise DependencyUnavailableError(str(exc)) from exc
except Exception as exc: # noqa: BLE001 - reported, not swallowed
+ budget_message = ai_service.budget_error_message(exc)
+ if budget_message:
+ if conv is not None:
+ ai_service.persist_conversation(row, conv, page_context=page_context)
+ return jsonify({'error': budget_message, 'code': 'ai_budget_exceeded',
+ 'conversation_id': row.id, 'usage': conv.usage if conv else {}}), 409
return unexpected_response(exc)
_persist_assistant_message(row, reply, tool_calls=[], usage=conv.usage)
@@ -461,6 +479,8 @@ def _run_chat(row, user, mode, page_context, attachment_refs, message, safe_mess
@ai_bp.route('/chat/stream', methods=['POST'])
@jwt_required()
def chat_stream():
+ if not ai_service.is_enabled():
+ return jsonify({'error': 'AI assistant is disabled. Enable it in Settings > AI Assistant.'}), 503
ai_service.ensure_initialized()
user = get_current_user()
if not ai_service.is_configured():
@@ -575,21 +595,28 @@ def chat_stream():
last_usage = payload['usage']
emit(name, payload)
except Exception as exc:
- logger.exception("AI stream worker failed")
- emit('error', {'message': str(exc) if isinstance(exc, ai_service.AIProtectionError)
- else 'AI request failed. Please try again.'})
+ budget_message = ai_service.budget_error_message(exc)
+ if budget_message:
+ emit('error', {'message': budget_message, 'code': 'ai_budget_exceeded'})
+ else:
+ logger.exception("AI stream worker failed")
+ emit('error', {'message': str(exc) if isinstance(exc, ai_service.AIProtectionError)
+ else 'AI request failed. Please try again.'})
finally:
ai_service.unregister_gate(conversation_id, gate)
try:
text = ''.join(acc_text)
- if text or tool_order:
+ if conv is not None:
+ last_usage = conv.usage
+ if text or tool_order or conv is not None:
conv_row = db.session.get(AiConversation, conversation_id)
if conv_row is not None:
- _persist_assistant_message(
- conv_row, text,
- tool_calls=[tool_calls[i] for i in tool_order if i in tool_calls],
- usage=last_usage,
- )
+ if text or tool_order:
+ _persist_assistant_message(
+ conv_row, text,
+ tool_calls=[tool_calls[i] for i in tool_order if i in tool_calls],
+ usage=last_usage,
+ )
if conv is not None:
ai_service.persist_conversation(conv_row, conv, page_context=page_context)
except Exception:
diff --git a/backend/app/services/ai_service.py b/backend/app/services/ai_service.py
index 9ba8fd93..b4325244 100644
--- a/backend/app/services/ai_service.py
+++ b/backend/app/services/ai_service.py
@@ -300,6 +300,14 @@ class AIProtectionError(RuntimeError):
"""Enabled protection failed; never send the original data as a fallback."""
+def budget_error_message(exc: Exception) -> Optional[str]:
+ """Translate SDK budget stops without exposing provider exception details."""
+ from prompture.exceptions import BudgetExceededError
+ if isinstance(exc, BudgetExceededError):
+ return 'This conversation has reached its AI cost limit. An administrator can adjust the limit in AI Assistant settings.'
+ return None
+
+
def _filter_secrets(text: str) -> str:
"""Deterministic credential filtering, independent of optional PII detection."""
text = re.sub(r'-----BEGIN [^-]*PRIVATE KEY-----.*?-----END [^-]*PRIVATE KEY-----',
@@ -553,10 +561,12 @@ def build_conversation(row, user, mode: str, page_context: Optional[dict],
# owned by Prompture. Reapply current policy instead of trusting export options.
class ConfiguredConversation(Conversation):
def __init__(self, **kwargs):
+ max_cost = _max_cost()
kwargs.update(driver=driver, model_name=resolved_model,
max_tool_rounds=(6 if mode == 'assistant' else 0),
- max_tool_result_length=4000, max_cost=_max_cost(),
- budget_policy=None, fallback_models=None, options={})
+ max_tool_result_length=4000, max_cost=max_cost,
+ budget_policy='hard_stop' if max_cost is not None else None,
+ fallback_models=None, options={})
super().__init__(**kwargs)
export = row.export
@@ -577,7 +587,7 @@ def build_conversation(row, user, mode: str, page_context: Optional[dict],
def _max_cost() -> Optional[float]:
- raw = _setting("ai_max_cost_usd", None)
+ raw = _setting("ai_max_cost_usd", 0.5)
try:
return float(raw) if raw not in (None, "", "0", 0) else None
except (TypeError, ValueError):
diff --git a/backend/tests/test_ai_attachments.py b/backend/tests/test_ai_attachments.py
index 14e40888..f4b1e822 100644
--- a/backend/tests/test_ai_attachments.py
+++ b/backend/tests/test_ai_attachments.py
@@ -169,6 +169,7 @@ def test_chat_persists_manifest_and_reauthorizes_each_turn(
ai_attachment_registry.register(
'test.resource', resolver, plugin_slug='test', replace=True,
)
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
monkeypatch.setattr(ai_service, 'injection_flagged', lambda message: False)
monkeypatch.setattr(ai_service, 'redact_input', lambda message: message)
@@ -217,6 +218,7 @@ def test_stream_emits_attachment_warnings(
def ask_live(self, message):
return iter(())
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
monkeypatch.setattr(ai_service, 'injection_flagged', lambda message: False)
monkeypatch.setattr(ai_service, 'redact_input', lambda message: message)
@@ -238,6 +240,7 @@ def test_stream_emits_attachment_warnings(
def test_chat_rejects_malformed_attachment_manifest(client, auth_headers, monkeypatch):
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
response = client.post('/api/v1/ai/chat', headers=auth_headers, json={
'message': 'Hello',
diff --git a/backend/tests/test_ai_connections.py b/backend/tests/test_ai_connections.py
index d400b651..6c816c48 100644
--- a/backend/tests/test_ai_connections.py
+++ b/backend/tests/test_ai_connections.py
@@ -276,6 +276,42 @@ def test_resume_restores_history_with_original_connection_not_ambient(app, monke
assert 'private-test-key' not in json.dumps(resumed.export())
[email protected]('streaming', [False, True])
[email protected]('resume', [False, True])
+def test_cost_limit_stops_driver_calls_in_fresh_and_resumed_chats(app, monkeypatch, streaming, resume):
+ from prompture.drivers.base import Driver
+ from prompture.exceptions import BudgetExceededError
+
+ class NoCallDriver(Driver):
+ def generate(self, prompt, options):
+ pytest.fail('A conversation at its cost limit must not call a provider')
+
+ monkeypatch.setattr(ai_service, 'build_tool_registry', lambda *a: None)
+ monkeypatch.setattr(ai_service, 'build_system_prompt', lambda *a, **kw: 'Policy')
+ monkeypatch.setattr(ai_service, '_maybe_redact_result', lambda data: data)
+ monkeypatch.setattr(service, 'build_driver', lambda *a: NoCallDriver())
+ with app.app_context():
+ SystemSettings.set('ai_max_cost_usd', '0.5')
+ connection = service.save(draft())
+ row = SimpleNamespace(id='budget-chat', connection_id=connection.id,
+ model_name='openai_compatible/auto', export=None)
+ conv = ai_service.build_conversation(row, None, 'simple', {}, None)
+ conv._usage['cost'] = 0.5
+ if resume:
+ row.export = conv.export()
+ conv = ai_service.build_conversation(row, None, 'simple', {}, None)
+ with pytest.raises(BudgetExceededError):
+ if streaming:
+ list(conv.ask_live('Continue'))
+ else:
+ conv.ask('Continue')
+ assert conv.budget_remaining['exceeded'] is True
+ # Removing the ceiling must also apply when resuming saved policy.
+ row.export = conv.export()
+ SystemSettings.set('ai_max_cost_usd', '0')
+ assert ai_service.build_conversation(row, None, 'simple', {}, None).budget_remaining is None
+
+
def test_migration_preserves_legacy_key_and_conversation(app):
from alembic.migration import MigrationContext
from alembic.operations import Operations
diff --git a/backend/tests/test_ai_security_boundaries.py b/backend/tests/test_ai_security_boundaries.py
index 90f752e6..5ff5cd77 100644
--- a/backend/tests/test_ai_security_boundaries.py
+++ b/backend/tests/test_ai_security_boundaries.py
@@ -198,6 +198,7 @@ def test_enabled_protection_failure_never_returns_original(monkeypatch):
@pytest.mark.parametrize('route', ['/chat', '/chat/stream'])
def test_chat_bounds_and_visible_protection_failure(client, auth_headers, monkeypatch, route):
monkeypatch.setattr(ai_service, 'ensure_initialized', lambda: None)
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
path = '/api/v1/ai' + route
invalid = client.post(path, headers=auth_headers, json={'message': 42})
@@ -218,6 +219,7 @@ def test_chat_bounds_and_visible_protection_failure(client, auth_headers, monkey
def test_chat_busy_uses_typed_http_error(client, auth_headers, monkeypatch, route):
from app.api import ai
monkeypatch.setattr(ai_service, 'ensure_initialized', lambda: None)
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
monkeypatch.setattr(ai_service, 'injection_flagged', lambda text: False)
monkeypatch.setattr(ai_service, 'redact_input', lambda text: text)
@@ -303,6 +305,7 @@ def test_disconnecting_at_open_cancels_stream_and_releases_slot(client, auth_hea
monkeypatch.setattr(ai, '_release_turn', release)
monkeypatch.setattr(ai_service, 'ensure_initialized', lambda: None)
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
monkeypatch.setattr(ai_service, 'injection_flagged', lambda message: False)
monkeypatch.setattr(ai_service, 'redact_input', lambda message: message)
@@ -334,3 +337,55 @@ def test_read_exception_does_not_leak_credentials_to_model(app, no_pii):
raise RuntimeError('Cannot connect to mysql://root:secret@host/db')
result = ai_service._make_read_wrapper(descriptor(broken), user)()
assert result == 'The tool failed to retrieve data.'
+
+
[email protected]('route', ['/chat', '/chat/stream'])
+def test_disabled_assistant_blocks_new_turns(client, auth_headers, monkeypatch, route):
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: False)
+ monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
+ def unexpected_init():
+ pytest.fail('Disabled chat must not initialize a provider or run tools')
+ monkeypatch.setattr(ai_service, 'ensure_initialized', unexpected_init)
+ response = client.post('/api/v1/ai' + route, headers=auth_headers, json={'message': 'hello'})
+ assert response.status_code == 503
+ assert 'disabled' in response.json['error']
+
+
[email protected]('route', ['/chat', '/chat/stream'])
+def test_budget_stop_is_clear_and_preserves_spend(client, auth_headers, monkeypatch, route):
+ from prompture.exceptions import BudgetExceededError
+ saved = []
+
+ class BudgetConversation:
+ usage = {'cost': 0.5, 'total_tokens': 100}
+
+ def ask(self, message):
+ raise BudgetExceededError('Internal provider details must not be returned')
+
+ def ask_live(self, message):
+ raise BudgetExceededError('Internal provider details must not be returned')
+ yield # The real streaming SDK raises while iterating.
+
+ monkeypatch.setattr(ai_service, 'ensure_initialized', lambda: None)
+ monkeypatch.setattr(ai_service, 'is_enabled', lambda: True)
+ monkeypatch.setattr(ai_service, 'is_configured', lambda: True)
+ monkeypatch.setattr(ai_service, 'injection_flagged', lambda message: False)
+ monkeypatch.setattr(ai_service, 'redact_input', lambda message: message)
+ monkeypatch.setattr(ai_service, 'build_conversation', lambda *a, **kw: BudgetConversation())
+ monkeypatch.setattr(ai_service, 'persist_conversation', lambda row, conv, **kw: saved.append(conv.usage))
+ response = client.post('/api/v1/ai' + route, headers=auth_headers,
+ json={'message': 'Continue'}, buffered=True)
+ assert response.status_code == (200 if route.endswith('stream') else 409)
+ body = response.get_data(as_text=True)
+ assert 'ai_budget_exceeded' in body and 'cost limit' in body
+ assert 'Internal provider' not in body
+ assert saved == [{'cost': 0.5, 'total_tokens': 100}]
+ if route.endswith('stream'):
+ assert 'event: done' in body
+
+
[email protected]('value', [-1, 'NaN', 'Infinity', '', None, True])
+def test_invalid_cost_limit_cannot_be_saved(client, auth_headers, value):
+ response = client.put('/api/v1/ai/settings', headers=auth_headers,
+ json={'max_cost_usd': value})
+ assert response.status_code == 400