agentlighthouse-core 0.4.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.
Files changed (78) hide show
  1. agentlighthouse_core-0.4.0.dist-info/METADATA +358 -0
  2. agentlighthouse_core-0.4.0.dist-info/RECORD +78 -0
  3. agentlighthouse_core-0.4.0.dist-info/WHEEL +4 -0
  4. agentlighthouse_core-0.4.0.dist-info/entry_points.txt +2 -0
  5. agentlighthouse_core-0.4.0.dist-info/licenses/LICENSE +21 -0
  6. al_core/__init__.py +12 -0
  7. al_core/_bundled/configs/audit.yaml +12 -0
  8. al_core/_bundled/configs/balanced.yaml +36 -0
  9. al_core/_bundled/configs/strict.yaml +32 -0
  10. al_core/_bundled/dashboard/assets/index-CN2LU6nV.css +1 -0
  11. al_core/_bundled/dashboard/assets/index-DX6cZzRi.js +40 -0
  12. al_core/_bundled/dashboard/index.html +15 -0
  13. al_core/_bundled/policies/default-deny.yaml +37 -0
  14. al_core/a2a/__init__.py +25 -0
  15. al_core/a2a/envelope.py +60 -0
  16. al_core/a2a/mediator.py +221 -0
  17. al_core/app.py +427 -0
  18. al_core/audit/__init__.py +149 -0
  19. al_core/audit/db.py +481 -0
  20. al_core/audit/pg.py +350 -0
  21. al_core/audit/pool.py +260 -0
  22. al_core/audit/siem.py +97 -0
  23. al_core/capability/__init__.py +28 -0
  24. al_core/capability/budget.py +56 -0
  25. al_core/capability/gate.py +334 -0
  26. al_core/capability/hitl.py +331 -0
  27. al_core/capability/policy.py +270 -0
  28. al_core/capability/store.py +209 -0
  29. al_core/capability/taint.py +79 -0
  30. al_core/cli/__init__.py +1 -0
  31. al_core/cli/main.py +1275 -0
  32. al_core/config/__init__.py +436 -0
  33. al_core/config/watch.py +51 -0
  34. al_core/detect/__init__.py +23 -0
  35. al_core/detect/base.py +131 -0
  36. al_core/detect/bip39_words.py +267 -0
  37. al_core/detect/budget.py +92 -0
  38. al_core/detect/detect_secrets_adapter.py +110 -0
  39. al_core/detect/engine.py +270 -0
  40. al_core/detect/scanners.py +454 -0
  41. al_core/egress/__init__.py +27 -0
  42. al_core/egress/nftables.py +59 -0
  43. al_core/egress/selftest.py +94 -0
  44. al_core/embed.py +51 -0
  45. al_core/gateway/__init__.py +29 -0
  46. al_core/gateway/content.py +154 -0
  47. al_core/gateway/decision.py +124 -0
  48. al_core/gateway/dnspin.py +131 -0
  49. al_core/gateway/forward.py +261 -0
  50. al_core/gateway/policy.py +66 -0
  51. al_core/gateway/ssrf.py +178 -0
  52. al_core/gateway/vkeys.py +205 -0
  53. al_core/gateway/web.py +801 -0
  54. al_core/identity/__init__.py +209 -0
  55. al_core/jsonstore.py +73 -0
  56. al_core/keys/__init__.py +141 -0
  57. al_core/killswitch.py +130 -0
  58. al_core/learn/__init__.py +17 -0
  59. al_core/learn/bundle.py +76 -0
  60. al_core/learn/miner.py +156 -0
  61. al_core/learn/scanner.py +77 -0
  62. al_core/learn/testgen.py +107 -0
  63. al_core/mcp/__init__.py +25 -0
  64. al_core/mcp/chain.py +147 -0
  65. al_core/mcp/descriptors.py +111 -0
  66. al_core/mcp/mediator.py +150 -0
  67. al_core/mcp/proxy.py +161 -0
  68. al_core/mcp/session.py +537 -0
  69. al_core/memory/__init__.py +15 -0
  70. al_core/memory/guard.py +330 -0
  71. al_core/memory/store.py +80 -0
  72. al_core/normalize/__init__.py +254 -0
  73. al_core/principal.py +59 -0
  74. al_core/receipt/__init__.py +192 -0
  75. al_core/resources.py +33 -0
  76. al_core/runtime.py +471 -0
  77. al_core/skills/__init__.py +30 -0
  78. al_core/skills/guard.py +218 -0
@@ -0,0 +1,209 @@
1
+ """SQLite persistence for the L4 gates' live state: pending HITL approvals and
2
+ taint marks, rehydrated on boot. Pending approvals survive a restart so an
3
+ in-flight production approval is not silently dropped; taint survives because
4
+ it is monotonic within a session — a restart must not launder it.
5
+
6
+ Deadlines are the subtle part: the gates measure time with a **monotonic**
7
+ clock, whose epoch resets every process. The store therefore keeps the wall
8
+ time of submission, and rehydration re-expresses it in the new process's
9
+ monotonic clock preserving the *elapsed age*, so a normally-running clock can
10
+ never extend a deadline. Wall time itself can step backward (NTP correction,
11
+ VM snapshot restore); a row whose age computes negative is treated as lapsed —
12
+ fail closed. A backward step smaller than the elapsed age undercounts it by at
13
+ most the step; the deadline is still bounded by the original timeout.
14
+
15
+ The store is also the crossing point between planes: the data plane files a
16
+ request, the control plane (another process, pointed at the same file via
17
+ ``control.dataplane_dir``) resolves it, and the data plane executes the call.
18
+ A resolution therefore updates the row rather than deleting it. An approval is
19
+ a **grant**: bound to the actor, tool, argument digest and session that filed
20
+ it, valid for ``timeout_s`` after it was given, and consumed exactly once by an
21
+ atomic delete — two consumers racing on one file cannot both win. A denial
22
+ stays readable for the same window so the filing plane can report it. Lapsed
23
+ requests, spent grants and aged-out denials are pruned — the table holds live
24
+ state, never history; receipts remain the record of what was decided.
25
+
26
+ Like the mirror, the single connection is opened ``check_same_thread=False``
27
+ and guarded by a lock; writes are one-per-decision, so contention is nil.
28
+ This is state, not evidence — receipts remain the audit trail.
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import sqlite3
34
+ import threading
35
+ import time
36
+ from pathlib import Path
37
+
38
+ _SCHEMA = """
39
+ CREATE TABLE IF NOT EXISTS approval_requests (
40
+ request_id TEXT PRIMARY KEY,
41
+ actor TEXT NOT NULL,
42
+ tool TEXT NOT NULL,
43
+ created_at_wall REAL NOT NULL,
44
+ timeout_s REAL NOT NULL,
45
+ status TEXT NOT NULL,
46
+ resolved_by TEXT,
47
+ resolved_at_wall REAL,
48
+ detail TEXT,
49
+ session TEXT,
50
+ args_digest TEXT
51
+ );
52
+ CREATE TABLE IF NOT EXISTS taint (
53
+ session_id TEXT NOT NULL,
54
+ source TEXT NOT NULL,
55
+ marked_at REAL NOT NULL,
56
+ PRIMARY KEY (session_id, source)
57
+ );
58
+ """
59
+
60
+
61
+ #: File name of the approvals store inside ``control.approvals_dir``.
62
+ APPROVALS_DB = "approvals.db"
63
+
64
+
65
+ class CapabilityStore:
66
+ """Persistent state for the L4 gates. One file, two tables.
67
+
68
+ A deployment that splits the planes opens two of these: the data plane's
69
+ private one (taint), and a second in ``control.approvals_dir`` that both
70
+ planes can write and that holds approvals only."""
71
+
72
+ def __init__(self, path: str | Path) -> None:
73
+ self.path = Path(path)
74
+ self.path.parent.mkdir(parents=True, exist_ok=True)
75
+ self._conn = sqlite3.connect(self.path, check_same_thread=False)
76
+ self._conn.execute("PRAGMA journal_mode=WAL")
77
+ self._lock = threading.Lock()
78
+ with self._lock, self._conn:
79
+ self._conn.executescript(_SCHEMA)
80
+
81
+ # -- approvals ---------------------------------------------------------
82
+
83
+ def save_approval(
84
+ self, request_id: str, actor: str, tool: str, timeout_s: float,
85
+ *, created_at_wall: float | None = None,
86
+ detail: str | None = None, session: str | None = None,
87
+ args_digest: str | None = None,
88
+ ) -> None:
89
+ with self._lock, self._conn:
90
+ self._conn.execute(
91
+ "INSERT OR REPLACE INTO approval_requests "
92
+ "(request_id, actor, tool, created_at_wall, timeout_s, status, "
93
+ " resolved_by, resolved_at_wall, detail, session, args_digest) "
94
+ "VALUES (?, ?, ?, ?, ?, 'pending', NULL, NULL, ?, ?, ?)",
95
+ (request_id, actor, tool, created_at_wall or time.time(), timeout_s,
96
+ detail, session, args_digest),
97
+ )
98
+
99
+ def resolve_approval(self, request_id: str, status: str, resolved_by: str | None) -> bool:
100
+ """Resolve a still-pending, unlapsed request. False when another
101
+ process resolved it first or its deadline passed — the caller lost."""
102
+ now = time.time()
103
+ with self._lock, self._conn:
104
+ cur = self._conn.execute(
105
+ "UPDATE approval_requests SET status = ?, resolved_by = ?, "
106
+ "resolved_at_wall = ? WHERE request_id = ? AND status = 'pending' "
107
+ "AND created_at_wall <= ? AND ? - created_at_wall < timeout_s",
108
+ (status, resolved_by, now, request_id, now, now),
109
+ )
110
+ return cur.rowcount == 1
111
+
112
+ def consume_approval(self, request_id: str) -> bool:
113
+ """Spend an approval grant. The delete is the atomic claim: exactly one
114
+ caller, in any process, gets True for a given grant."""
115
+ now = time.time()
116
+ with self._lock, self._conn:
117
+ cur = self._conn.execute(
118
+ "DELETE FROM approval_requests WHERE request_id = ? "
119
+ "AND status = 'approved' AND resolved_at_wall <= ? "
120
+ "AND ? - resolved_at_wall < timeout_s",
121
+ (request_id, now, now),
122
+ )
123
+ return cur.rowcount == 1
124
+
125
+ def approval(self, request_id: str) -> dict | None:
126
+ """One live row (see :meth:`approval_rows`), or None."""
127
+ rows = self._live_rows("WHERE request_id = ?", (request_id,))
128
+ return rows[0] if rows else None
129
+
130
+ def load_approvals(self) -> list[dict]:
131
+ """The pending approvals, each with ``age_s`` — seconds elapsed since
132
+ the original wall-clock submission, so a rehydrating gate can re-anchor
133
+ the request in its own monotonic clock without moving the deadline.
134
+
135
+ Wall time can step backward between submission and this read (NTP
136
+ correction, VM snapshot restore); a row whose age comes out negative is
137
+ from "the future" and is dropped as lapsed — fail closed, never a fresh
138
+ deadline. Rows past their window are pruned here too, so the table
139
+ never accumulates history.
140
+ """
141
+ return [r for r in self._live_rows("", ()) if r["status"] == "pending"]
142
+
143
+ def live_approvals(self) -> list[dict]:
144
+ """Every row still inside its window: pending requests, unspent
145
+ grants and recent denials (see :meth:`_live_rows`)."""
146
+ return self._live_rows("", ())
147
+
148
+ def _live_rows(self, where: str, params: tuple) -> list[dict]:
149
+ """Rows still inside their window, each with ``age_s`` and (once
150
+ resolved) ``resolved_age_s``. Expired rows are deleted on the way."""
151
+ now = time.time()
152
+ rows_out: list[dict] = []
153
+ with self._lock, self._conn:
154
+ rows = self._conn.execute(
155
+ "SELECT request_id, actor, tool, created_at_wall, timeout_s, status, "
156
+ "resolved_by, resolved_at_wall, detail, session, args_digest "
157
+ f"FROM approval_requests {where}", params,
158
+ ).fetchall()
159
+ for (request_id, actor, tool, created_at_wall, timeout_s, status,
160
+ resolved_by, resolved_at_wall, detail, session, args_digest) in rows:
161
+ age_s = now - created_at_wall
162
+ if status == "pending":
163
+ expired = age_s < 0 or age_s >= timeout_s
164
+ resolved_age_s = None
165
+ else:
166
+ resolved_age_s = now - (resolved_at_wall or 0.0)
167
+ expired = resolved_age_s < 0 or resolved_age_s >= timeout_s
168
+ if expired:
169
+ self._conn.execute(
170
+ "DELETE FROM approval_requests WHERE request_id = ?", (request_id,))
171
+ continue
172
+ rows_out.append({
173
+ "request_id": request_id, "actor": actor, "tool": tool,
174
+ "age_s": age_s, "timeout_s": timeout_s, "status": status,
175
+ "resolved_by": resolved_by, "resolved_age_s": resolved_age_s,
176
+ "detail": detail, "session": session, "args_digest": args_digest,
177
+ })
178
+ return rows_out
179
+
180
+ # -- taint ---------------------------------------------------------------
181
+
182
+ def mark_taint(self, session_id: str, source: str) -> None:
183
+ with self._lock, self._conn:
184
+ self._conn.execute(
185
+ "INSERT OR IGNORE INTO taint (session_id, source, marked_at) VALUES (?, ?, ?)",
186
+ (session_id, source, time.time()),
187
+ )
188
+
189
+ def clear_taint(self, session_id: str) -> None:
190
+ with self._lock, self._conn:
191
+ self._conn.execute("DELETE FROM taint WHERE session_id = ?", (session_id,))
192
+
193
+ def load_taint(self) -> dict[str, list[str]]:
194
+ """session_id -> sources, in the order they were marked."""
195
+ with self._lock:
196
+ rows = self._conn.execute(
197
+ "SELECT session_id, source FROM taint ORDER BY marked_at, rowid"
198
+ ).fetchall()
199
+ sessions: dict[str, list[str]] = {}
200
+ for session_id, source in rows:
201
+ sessions.setdefault(session_id, []).append(source)
202
+ return sessions
203
+
204
+ def close(self) -> None:
205
+ with self._lock:
206
+ self._conn.close()
207
+
208
+
209
+ __all__ = ["APPROVALS_DB", "CapabilityStore"]
@@ -0,0 +1,79 @@
1
+ """Taint tracking (L4) — untrusted ingestion tightens later decisions (ASI01).
2
+
3
+ A session that ingests untrusted content — a web fetch, an unpinned tool
4
+ result, content that tripped an injection scanner — becomes **tainted**. A
5
+ tainted session then faces tightened policy on subsequent *protected*
6
+ operations (the HITL gate widens; scanning escalates). This is the mechanism
7
+ that stops an indirect prompt injection read early in a session from quietly
8
+ driving an exfiltration later in it.
9
+
10
+ Taint is per-session, monotonic within a session (it does not clear itself —
11
+ only an explicit ``clear`` resets it), and records *why* it was set so the
12
+ receipt can explain the escalation.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from dataclasses import dataclass, field
18
+ from typing import TYPE_CHECKING
19
+
20
+ if TYPE_CHECKING:
21
+ from .store import CapabilityStore
22
+
23
+
24
+ class TaintSource:
25
+ """Fixed vocabulary for why a session became tainted."""
26
+
27
+ WEB_FETCH = "web_fetch" # ingested fetched web content
28
+ UNPINNED_TOOL_RESULT = "unpinned_tool" # tool output not descriptor-pinned
29
+ INJECTION_DETECTED = "injection" # content gate flagged injection
30
+ UNTRUSTED_AGENT = "untrusted_agent" # A2A / inter-agent message
31
+ MEMORY_POISON = "memory_poison" # hostile content found in the memory store
32
+
33
+
34
+ @dataclass
35
+ class TaintState:
36
+ tainted: bool = False
37
+ sources: list[str] = field(default_factory=list)
38
+
39
+
40
+ class TaintTracker:
41
+ """Per-session taint state. Thread-safety is the caller's concern (the
42
+ gateway serializes receipt writes; taint updates ride the same path)."""
43
+
44
+ def __init__(self, *, store: CapabilityStore | None = None) -> None:
45
+ self._sessions: dict[str, TaintState] = {}
46
+ self._store = store
47
+ if store is not None:
48
+ # Rehydrate: taint is monotonic within a session, and a restart
49
+ # must not launder it (that direction is fail-open).
50
+ for session_id, sources in store.load_taint().items():
51
+ self._sessions[session_id] = TaintState(
52
+ tainted=True, sources=list(sources))
53
+
54
+ def mark(self, session_id: str, source: str) -> None:
55
+ """Taint ``session_id`` (idempotent per source)."""
56
+ state = self._sessions.setdefault(session_id, TaintState())
57
+ state.tainted = True
58
+ if source not in state.sources:
59
+ state.sources.append(source)
60
+ if self._store is not None:
61
+ self._store.mark_taint(session_id, source)
62
+
63
+ def is_tainted(self, session_id: str) -> bool:
64
+ state = self._sessions.get(session_id)
65
+ return bool(state and state.tainted)
66
+
67
+ def state(self, session_id: str) -> TaintState:
68
+ return self._sessions.get(session_id, TaintState())
69
+
70
+ def sources(self, session_id: str) -> list[str]:
71
+ return list(self.state(session_id).sources)
72
+
73
+ def clear(self, session_id: str) -> None:
74
+ self._sessions.pop(session_id, None)
75
+ if self._store is not None:
76
+ self._store.clear_taint(session_id)
77
+
78
+
79
+ __all__ = ["TaintSource", "TaintState", "TaintTracker"]
@@ -0,0 +1 @@
1
+ """AgentLighthouse CLI package."""