admina-framework 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- admina/__init__.py +34 -0
- admina/cli/__init__.py +14 -0
- admina/cli/commands/__init__.py +14 -0
- admina/cli/main.py +1522 -0
- admina/cli/templates/admina.yaml.j2 +77 -0
- admina/cli/templates/docker-compose.yml.j2 +254 -0
- admina/cli/templates/env.j2 +10 -0
- admina/cli/templates/main.py.j2 +95 -0
- admina/cli/templates/plugin.py.j2 +145 -0
- admina/cli/templates/plugin_pyproject.toml.j2 +15 -0
- admina/cli/templates/plugin_readme.md.j2 +27 -0
- admina/cli/templates/plugin_test.py.j2 +48 -0
- admina/core/__init__.py +14 -0
- admina/core/config.py +497 -0
- admina/core/event_bus.py +112 -0
- admina/core/secrets.py +257 -0
- admina/core/types.py +146 -0
- admina/dashboard/__init__.py +8 -0
- admina/dashboard/static/heimdall.png +0 -0
- admina/dashboard/static/index.html +1045 -0
- admina/dashboard/static/vendor/alpinejs.min.js +5 -0
- admina/domains/__init__.py +14 -0
- admina/domains/agent_security/__init__.py +41 -0
- admina/domains/agent_security/firewall.py +634 -0
- admina/domains/agent_security/loop_breaker.py +176 -0
- admina/domains/ai_infra/__init__.py +79 -0
- admina/domains/ai_infra/llm_engine.py +477 -0
- admina/domains/ai_infra/rag.py +817 -0
- admina/domains/ai_infra/webui.py +292 -0
- admina/domains/compliance/__init__.py +109 -0
- admina/domains/compliance/cross_regulation.py +314 -0
- admina/domains/compliance/eu_ai_act.py +367 -0
- admina/domains/compliance/forensic.py +380 -0
- admina/domains/compliance/gdpr.py +331 -0
- admina/domains/compliance/nis2.py +258 -0
- admina/domains/compliance/oisg.py +658 -0
- admina/domains/compliance/otel.py +101 -0
- admina/domains/data_sovereignty/__init__.py +42 -0
- admina/domains/data_sovereignty/classification.py +102 -0
- admina/domains/data_sovereignty/pii.py +260 -0
- admina/domains/data_sovereignty/residency.py +121 -0
- admina/integrations/__init__.py +14 -0
- admina/integrations/_engines.py +63 -0
- admina/integrations/cheshirecat/__init__.py +13 -0
- admina/integrations/cheshirecat/admina-plugin/admina_governance.py +207 -0
- admina/integrations/crewai/__init__.py +13 -0
- admina/integrations/crewai/callbacks.py +347 -0
- admina/integrations/langchain/__init__.py +13 -0
- admina/integrations/langchain/callbacks.py +341 -0
- admina/integrations/n8n/__init__.py +14 -0
- admina/integrations/openclaw/__init__.py +14 -0
- admina/plugins/__init__.py +49 -0
- admina/plugins/base.py +633 -0
- admina/plugins/builtin/__init__.py +14 -0
- admina/plugins/builtin/adapters/__init__.py +14 -0
- admina/plugins/builtin/adapters/ollama.py +120 -0
- admina/plugins/builtin/adapters/openai.py +138 -0
- admina/plugins/builtin/alerts/__init__.py +14 -0
- admina/plugins/builtin/alerts/log.py +66 -0
- admina/plugins/builtin/alerts/webhook.py +102 -0
- admina/plugins/builtin/auth/__init__.py +14 -0
- admina/plugins/builtin/auth/apikey.py +138 -0
- admina/plugins/builtin/compliance/__init__.py +14 -0
- admina/plugins/builtin/compliance/eu_ai_act.py +202 -0
- admina/plugins/builtin/connectors/__init__.py +14 -0
- admina/plugins/builtin/connectors/chromadb.py +137 -0
- admina/plugins/builtin/connectors/filesystem.py +111 -0
- admina/plugins/builtin/forensic/__init__.py +14 -0
- admina/plugins/builtin/forensic/filesystem.py +163 -0
- admina/plugins/builtin/forensic/minio.py +180 -0
- admina/plugins/builtin/guards/__init__.py +0 -0
- admina/plugins/builtin/guards/guardrailsai_guard.py +172 -0
- admina/plugins/builtin/pii/__init__.py +14 -0
- admina/plugins/builtin/pii/spacy_regex.py +160 -0
- admina/plugins/builtin/transports/__init__.py +14 -0
- admina/plugins/builtin/transports/http_rest.py +97 -0
- admina/plugins/builtin/transports/mcp.py +173 -0
- admina/plugins/registry.py +356 -0
- admina/proxy/__init__.py +15 -0
- admina/proxy/api/__init__.py +17 -0
- admina/proxy/api/dashboard.py +925 -0
- admina/proxy/api/integration.py +153 -0
- admina/proxy/config.py +214 -0
- admina/proxy/engine_bridge.py +306 -0
- admina/proxy/governance.py +232 -0
- admina/proxy/main.py +1484 -0
- admina/proxy/multi_upstream.py +156 -0
- admina/proxy/state.py +97 -0
- admina/py.typed +0 -0
- admina/sdk/__init__.py +34 -0
- admina/sdk/_compat.py +43 -0
- admina/sdk/compliance_kit.py +359 -0
- admina/sdk/governed_agent.py +391 -0
- admina/sdk/governed_data.py +434 -0
- admina/sdk/governed_model.py +241 -0
- admina_framework-0.9.0.dist-info/METADATA +575 -0
- admina_framework-0.9.0.dist-info/RECORD +102 -0
- admina_framework-0.9.0.dist-info/WHEEL +5 -0
- admina_framework-0.9.0.dist-info/entry_points.txt +2 -0
- admina_framework-0.9.0.dist-info/licenses/LICENSE +191 -0
- admina_framework-0.9.0.dist-info/licenses/NOTICE +16 -0
- admina_framework-0.9.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Copyright © 2025–2026 Stefano Noferi & Admina contributors
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
Admina — Loop Breaker Engine — Agent Security domain
|
|
17
|
+
Cosine-similarity sliding window detects reasoning loops in real-time.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import hashlib
|
|
21
|
+
import logging
|
|
22
|
+
import re
|
|
23
|
+
from collections import defaultdict
|
|
24
|
+
|
|
25
|
+
import numpy as np
|
|
26
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
27
|
+
from sklearn.metrics.pairwise import cosine_similarity
|
|
28
|
+
|
|
29
|
+
from admina.core.types import RiskLevel
|
|
30
|
+
|
|
31
|
+
# Default thresholds — overridden per-instance via constructor kwargs.
|
|
32
|
+
_DEFAULT_WINDOW_SIZE = 10
|
|
33
|
+
_DEFAULT_SIMILARITY_THRESHOLD = 0.85
|
|
34
|
+
_DEFAULT_MAX_CONSECUTIVE = 3
|
|
35
|
+
|
|
36
|
+
logger = logging.getLogger("admina.loop_breaker")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class LoopBreaker:
|
|
40
|
+
"""
|
|
41
|
+
Maintains a sliding window of recent requests per session.
|
|
42
|
+
Uses TF-IDF + cosine similarity to detect repetitive patterns.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
window_size: int | None = None,
|
|
48
|
+
similarity_threshold: float | None = None,
|
|
49
|
+
max_consecutive: int | None = None,
|
|
50
|
+
**kwargs,
|
|
51
|
+
):
|
|
52
|
+
self._window_size = window_size or _DEFAULT_WINDOW_SIZE
|
|
53
|
+
self._threshold = similarity_threshold or _DEFAULT_SIMILARITY_THRESHOLD
|
|
54
|
+
self._max_consecutive = max_consecutive or _DEFAULT_MAX_CONSECUTIVE
|
|
55
|
+
self.windows: dict[str, list[str]] = defaultdict(list)
|
|
56
|
+
self.vectorizer = TfidfVectorizer(
|
|
57
|
+
max_features=500,
|
|
58
|
+
stop_words="english",
|
|
59
|
+
ngram_range=(1, 2),
|
|
60
|
+
)
|
|
61
|
+
self.consecutive_similar: dict[str, int] = defaultdict(int)
|
|
62
|
+
self.total_blocked: int = 0
|
|
63
|
+
|
|
64
|
+
def _content_hash(self, text: str) -> str:
|
|
65
|
+
return hashlib.sha256(text.encode()).hexdigest()[:16]
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def _variable_tokens(text: str) -> set[str]:
|
|
69
|
+
"""Tokens that typically *differ* between iterations of a legitimate
|
|
70
|
+
template (numbers, hex hashes, URLs, UUID-like strings)."""
|
|
71
|
+
toks: set[str] = set()
|
|
72
|
+
toks.update(re.findall(r"\b\d+\b", text))
|
|
73
|
+
toks.update(re.findall(r"\b[a-f0-9]{8,}\b", text, re.IGNORECASE))
|
|
74
|
+
toks.update(re.findall(r"https?://\S+", text))
|
|
75
|
+
toks.update(
|
|
76
|
+
re.findall(
|
|
77
|
+
r"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b",
|
|
78
|
+
text,
|
|
79
|
+
re.IGNORECASE,
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
return toks
|
|
83
|
+
|
|
84
|
+
def _compute_similarity(self, texts: list[str]) -> float:
|
|
85
|
+
"""Average pairwise cosine similarity in the window.
|
|
86
|
+
|
|
87
|
+
If the texts are textually similar (>= threshold) but the *variable*
|
|
88
|
+
tokens (numbers, IDs, URLs) differ across messages, the similarity
|
|
89
|
+
is damped by a fixed factor: this is the signature of a legitimate
|
|
90
|
+
action template ("OK, I will proceed with task 1/2/3 ...") rather
|
|
91
|
+
than a real reasoning loop ("I retrieve the file" repeated).
|
|
92
|
+
"""
|
|
93
|
+
if len(texts) < 2:
|
|
94
|
+
return 0.0
|
|
95
|
+
try:
|
|
96
|
+
tfidf_matrix = self.vectorizer.fit_transform(texts)
|
|
97
|
+
sim_matrix = cosine_similarity(tfidf_matrix)
|
|
98
|
+
# Get upper triangle (exclude diagonal)
|
|
99
|
+
n = sim_matrix.shape[0]
|
|
100
|
+
upper = sim_matrix[np.triu_indices(n, k=1)]
|
|
101
|
+
raw_sim = float(np.mean(upper)) if len(upper) > 0 else 0.0
|
|
102
|
+
except ValueError as e:
|
|
103
|
+
logger.warning("Similarity computation failed: %s", e)
|
|
104
|
+
return 0.0
|
|
105
|
+
|
|
106
|
+
# Damping: if at least two messages have *different* variable-token
|
|
107
|
+
# sets, this looks like a counter/template loop, not a true repeat.
|
|
108
|
+
if raw_sim >= self._threshold:
|
|
109
|
+
var_sets = [self._variable_tokens(t) for t in texts]
|
|
110
|
+
distinct = {frozenset(s) for s in var_sets if s}
|
|
111
|
+
if len(distinct) >= 2:
|
|
112
|
+
# Damp by 30%; "OK proceed task 1/2/3" with sim≈1.0 → ≈0.70,
|
|
113
|
+
# below the default 0.85 threshold so it stops tripping.
|
|
114
|
+
return round(raw_sim * 0.7, 4)
|
|
115
|
+
return raw_sim
|
|
116
|
+
|
|
117
|
+
def check(self, session_id: str, content: str) -> dict:
|
|
118
|
+
"""
|
|
119
|
+
Check if the current request is part of a reasoning loop.
|
|
120
|
+
Returns: {is_loop: bool, similarity: float, risk_level: str, window_size: int}
|
|
121
|
+
"""
|
|
122
|
+
window = self.windows[session_id]
|
|
123
|
+
window.append(content)
|
|
124
|
+
|
|
125
|
+
# Keep window at configured size
|
|
126
|
+
if len(window) > self._window_size:
|
|
127
|
+
window.pop(0)
|
|
128
|
+
|
|
129
|
+
result = {
|
|
130
|
+
"is_loop": False,
|
|
131
|
+
"similarity": 0.0,
|
|
132
|
+
"risk_level": RiskLevel.LOW,
|
|
133
|
+
"window_size": len(window),
|
|
134
|
+
"consecutive_similar": 0,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if len(window) < 3:
|
|
138
|
+
return result
|
|
139
|
+
|
|
140
|
+
# Check similarity within the sliding window
|
|
141
|
+
similarity = self._compute_similarity(window[-5:]) # last 5 entries
|
|
142
|
+
result["similarity"] = round(similarity, 4)
|
|
143
|
+
|
|
144
|
+
if similarity >= self._threshold:
|
|
145
|
+
self.consecutive_similar[session_id] += 1
|
|
146
|
+
else:
|
|
147
|
+
self.consecutive_similar[session_id] = 0
|
|
148
|
+
|
|
149
|
+
consecutive = self.consecutive_similar[session_id]
|
|
150
|
+
result["consecutive_similar"] = consecutive
|
|
151
|
+
|
|
152
|
+
if consecutive >= self._max_consecutive:
|
|
153
|
+
result["is_loop"] = True
|
|
154
|
+
result["risk_level"] = RiskLevel.HIGH
|
|
155
|
+
self.total_blocked += 1
|
|
156
|
+
logger.warning(
|
|
157
|
+
"Loop detected for session %s: similarity=%.3f, consecutive=%d",
|
|
158
|
+
session_id,
|
|
159
|
+
similarity,
|
|
160
|
+
consecutive,
|
|
161
|
+
)
|
|
162
|
+
elif similarity >= self._threshold:
|
|
163
|
+
result["risk_level"] = RiskLevel.MEDIUM
|
|
164
|
+
|
|
165
|
+
return result
|
|
166
|
+
|
|
167
|
+
def reset_session(self, session_id: str) -> None:
|
|
168
|
+
"""Reset window for a session."""
|
|
169
|
+
self.windows.pop(session_id, None)
|
|
170
|
+
self.consecutive_similar.pop(session_id, None)
|
|
171
|
+
|
|
172
|
+
def get_stats(self) -> dict:
|
|
173
|
+
return {
|
|
174
|
+
"active_sessions": len(self.windows),
|
|
175
|
+
"total_blocked": self.total_blocked,
|
|
176
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Copyright © 2025–2026 Stefano Noferi & Admina contributors
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Admina — AI infrastructure domain.
|
|
16
|
+
|
|
17
|
+
Provides LLM engine, RAG pipeline, and Web UI integration modules.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from admina.domains.ai_infra.llm_engine import (
|
|
23
|
+
GPUInfo,
|
|
24
|
+
GPUVendor,
|
|
25
|
+
LLMBackend,
|
|
26
|
+
LLMEngine,
|
|
27
|
+
ModelStatus,
|
|
28
|
+
OllamaConfig,
|
|
29
|
+
VLLMConfig,
|
|
30
|
+
detect_gpu,
|
|
31
|
+
)
|
|
32
|
+
from admina.domains.ai_infra.rag import (
|
|
33
|
+
ChromaDBConfig,
|
|
34
|
+
Chunk,
|
|
35
|
+
ChunkingStrategy,
|
|
36
|
+
Document,
|
|
37
|
+
DocumentFormat,
|
|
38
|
+
EmbeddingBackend,
|
|
39
|
+
IngestResult,
|
|
40
|
+
RAGPipeline,
|
|
41
|
+
RetrievalResult,
|
|
42
|
+
)
|
|
43
|
+
from admina.domains.ai_infra.webui import (
|
|
44
|
+
AuthConfig,
|
|
45
|
+
AuthMode,
|
|
46
|
+
LDAPConfig,
|
|
47
|
+
OIDCConfig,
|
|
48
|
+
OpenWebUIConfig,
|
|
49
|
+
WebUIEngine,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
__all__ = [
|
|
53
|
+
# LLM engine
|
|
54
|
+
"GPUInfo",
|
|
55
|
+
"GPUVendor",
|
|
56
|
+
"LLMBackend",
|
|
57
|
+
"LLMEngine",
|
|
58
|
+
"ModelStatus",
|
|
59
|
+
"OllamaConfig",
|
|
60
|
+
"VLLMConfig",
|
|
61
|
+
"detect_gpu",
|
|
62
|
+
# RAG pipeline
|
|
63
|
+
"Chunk",
|
|
64
|
+
"ChromaDBConfig",
|
|
65
|
+
"ChunkingStrategy",
|
|
66
|
+
"Document",
|
|
67
|
+
"DocumentFormat",
|
|
68
|
+
"EmbeddingBackend",
|
|
69
|
+
"IngestResult",
|
|
70
|
+
"RAGPipeline",
|
|
71
|
+
"RetrievalResult",
|
|
72
|
+
# Web UI
|
|
73
|
+
"AuthConfig",
|
|
74
|
+
"AuthMode",
|
|
75
|
+
"LDAPConfig",
|
|
76
|
+
"OIDCConfig",
|
|
77
|
+
"OpenWebUIConfig",
|
|
78
|
+
"WebUIEngine",
|
|
79
|
+
]
|