agentboxx 0.2.1__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.
- agentbox/__init__.py +5 -0
- agentbox/agent.py +138 -0
- agentbox/cli.py +252 -0
- agentbox/docker_backend.py +167 -0
- agentbox/error_signatures.py +46 -0
- agentbox/executor.py +102 -0
- agentbox/loop_detector.py +71 -0
- agentbox/recorder.py +73 -0
- agentbox/redact.py +71 -0
- agentbox/report.py +266 -0
- agentbox/sandbox_stats.py +80 -0
- agentbox/signer.py +148 -0
- agentbox/streaming.py +133 -0
- agentbox/workspace.py +83 -0
- agentboxx-0.2.1.dist-info/METADATA +265 -0
- agentboxx-0.2.1.dist-info/RECORD +20 -0
- agentboxx-0.2.1.dist-info/WHEEL +5 -0
- agentboxx-0.2.1.dist-info/entry_points.txt +2 -0
- agentboxx-0.2.1.dist-info/licenses/LICENSE +201 -0
- agentboxx-0.2.1.dist-info/top_level.txt +1 -0
agentbox/signer.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Tamper-evident event log: every record is hash-chained and HMAC-signed."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import hmac
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import time
|
|
7
|
+
import uuid
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
GENESIS = "0" * 64
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def canon(obj) -> bytes:
|
|
14
|
+
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _sha(data: bytes) -> str:
|
|
18
|
+
return hashlib.sha256(data).hexdigest()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def load_or_create_key(path=None) -> bytes:
|
|
22
|
+
path = Path(path or Path.home() / ".agentbox" / "key")
|
|
23
|
+
if path.exists():
|
|
24
|
+
return bytes.fromhex(path.read_text().strip())
|
|
25
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
key = os.urandom(32)
|
|
27
|
+
path.write_text(key.hex())
|
|
28
|
+
path.chmod(0o600)
|
|
29
|
+
return key
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ChainWriter:
|
|
33
|
+
def __init__(self, path, key: bytes, overwrite: bool = False):
|
|
34
|
+
self.path = Path(path)
|
|
35
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
if self.path.exists() and self.path.stat().st_size > 0:
|
|
37
|
+
if not overwrite:
|
|
38
|
+
raise FileExistsError(
|
|
39
|
+
f"Log already exists: {self.path}. Use a new session id or pass overwrite=True.")
|
|
40
|
+
self.path.write_text("")
|
|
41
|
+
manifest_path(self.path).unlink(missing_ok=True) # never keep a stale manifest
|
|
42
|
+
self.key, self.seq, self.prev = key, 0, GENESIS
|
|
43
|
+
|
|
44
|
+
def write_manifest(self):
|
|
45
|
+
"""Write the signed closing manifest. Call after the final record."""
|
|
46
|
+
write_manifest(self.path, self.key, self.seq, self.prev)
|
|
47
|
+
|
|
48
|
+
def append(self, event_type: str, data: dict) -> dict:
|
|
49
|
+
self.seq += 1
|
|
50
|
+
event = {
|
|
51
|
+
"schema_version": "0.1",
|
|
52
|
+
"event_id": str(uuid.uuid4()),
|
|
53
|
+
"event_type": event_type,
|
|
54
|
+
"ts": time.time(),
|
|
55
|
+
"seq": self.seq,
|
|
56
|
+
"data": data,
|
|
57
|
+
}
|
|
58
|
+
chain_hash = _sha((self.prev + _sha(canon(event))).encode())
|
|
59
|
+
sig = hmac.new(self.key, chain_hash.encode(), hashlib.sha256).hexdigest()
|
|
60
|
+
record = {"event": event, "prev_hash": self.prev, "hash": chain_hash, "sig": sig}
|
|
61
|
+
with open(self.path, "a", encoding="utf-8") as f:
|
|
62
|
+
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
|
63
|
+
f.flush()
|
|
64
|
+
os.fsync(f.fileno())
|
|
65
|
+
self.prev = chain_hash
|
|
66
|
+
return record
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def verify_file(path, key: bytes) -> list[str]:
|
|
70
|
+
"""Return a list of problems. Empty list = log is intact."""
|
|
71
|
+
problems, prev, expected = [], GENESIS, 1
|
|
72
|
+
with open(path, encoding="utf-8") as f:
|
|
73
|
+
for n, line in enumerate(f, 1):
|
|
74
|
+
try:
|
|
75
|
+
rec = json.loads(line)
|
|
76
|
+
ev = rec["event"]
|
|
77
|
+
except (json.JSONDecodeError, KeyError):
|
|
78
|
+
problems.append(f"line {n}: unreadable record")
|
|
79
|
+
continue
|
|
80
|
+
if ev["seq"] != expected:
|
|
81
|
+
problems.append(f"line {n}: sequence gap (expected {expected}, got {ev['seq']})")
|
|
82
|
+
expected = ev["seq"] + 1
|
|
83
|
+
if rec["prev_hash"] != prev:
|
|
84
|
+
problems.append(f"line {n}: chain broken")
|
|
85
|
+
good = _sha((rec["prev_hash"] + _sha(canon(ev))).encode())
|
|
86
|
+
if good != rec["hash"]:
|
|
87
|
+
problems.append(f"line {n}: content was modified")
|
|
88
|
+
want = hmac.new(key, rec["hash"].encode(), hashlib.sha256).hexdigest()
|
|
89
|
+
if not hmac.compare_digest(want, rec["sig"]):
|
|
90
|
+
problems.append(f"line {n}: bad signature")
|
|
91
|
+
prev = rec["hash"]
|
|
92
|
+
return problems
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def manifest_path(log_path) -> Path:
|
|
96
|
+
p = Path(log_path)
|
|
97
|
+
return p.with_name(p.stem + ".manifest.json")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def write_manifest(log_path, key: bytes, records: int, final_hash: str):
|
|
101
|
+
"""Signed summary written when a session closes: record count and last hash."""
|
|
102
|
+
payload = {"log": Path(log_path).name, "records": records, "final_hash": final_hash}
|
|
103
|
+
sig = hmac.new(key, canon(payload), hashlib.sha256).hexdigest()
|
|
104
|
+
manifest_path(log_path).write_text(
|
|
105
|
+
json.dumps({"payload": payload, "sig": sig}, indent=2), encoding="utf-8")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def verify_log(path, key: bytes):
|
|
109
|
+
"""Full check. Returns (problems, incomplete).
|
|
110
|
+
|
|
111
|
+
problems = evidence of tampering (bad chain or signature, or the log does
|
|
112
|
+
not match its signed manifest, e.g. the end was cut off).
|
|
113
|
+
incomplete = the log is intact so far but was never closed, or its end is
|
|
114
|
+
missing: a crash, a running session, or a removed tail.
|
|
115
|
+
"""
|
|
116
|
+
problems = verify_file(path, key)
|
|
117
|
+
last = {}
|
|
118
|
+
with open(path, encoding="utf-8") as f:
|
|
119
|
+
for line in f:
|
|
120
|
+
try:
|
|
121
|
+
rec = json.loads(line)
|
|
122
|
+
last = {"seq": rec["event"]["seq"], "hash": rec["hash"],
|
|
123
|
+
"type": rec["event"]["event_type"]}
|
|
124
|
+
except (ValueError, KeyError, TypeError):
|
|
125
|
+
continue
|
|
126
|
+
incomplete = []
|
|
127
|
+
mp = manifest_path(path)
|
|
128
|
+
if mp.exists():
|
|
129
|
+
try:
|
|
130
|
+
m = json.loads(mp.read_text(encoding="utf-8"))
|
|
131
|
+
payload, sig = m["payload"], m["sig"]
|
|
132
|
+
want = hmac.new(key, canon(payload), hashlib.sha256).hexdigest()
|
|
133
|
+
if not hmac.compare_digest(want, sig):
|
|
134
|
+
problems.append("manifest signature is invalid")
|
|
135
|
+
elif last.get("seq") != payload["records"] or last.get("hash") != payload["final_hash"]:
|
|
136
|
+
problems.append(
|
|
137
|
+
f"log does not match its signed manifest (expected {payload['records']} "
|
|
138
|
+
f"records ending in {payload['final_hash'][:12]}): truncated or modified")
|
|
139
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
140
|
+
problems.append("manifest is unreadable")
|
|
141
|
+
else:
|
|
142
|
+
if last.get("type") != "session.end":
|
|
143
|
+
incomplete.append("no session.end record and no manifest: the session crashed, "
|
|
144
|
+
"is still running, or the end of the log was removed")
|
|
145
|
+
else:
|
|
146
|
+
incomplete.append("session is closed but its manifest is missing: "
|
|
147
|
+
"the manifest may have been deleted")
|
|
148
|
+
return problems, incomplete
|
agentbox/streaming.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Run a command with bounded memory, a hard deadline, and a whole-tree kill.
|
|
2
|
+
|
|
3
|
+
Why this exists: subprocess.run(capture_output=True) keeps all output in memory
|
|
4
|
+
(a command that prints gigabytes can exhaust the host), and a timeout only
|
|
5
|
+
kills the direct child, leaving anything it started running.
|
|
6
|
+
"""
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import signal
|
|
10
|
+
import subprocess
|
|
11
|
+
import threading
|
|
12
|
+
import time
|
|
13
|
+
|
|
14
|
+
HEAD_BYTES = 1900
|
|
15
|
+
TAIL_BYTES = 1900
|
|
16
|
+
DEFAULT_MAX_STREAM_BYTES = 10 * 1024 * 1024 # per stream (stdout, stderr)
|
|
17
|
+
_MAX_CUT_TOKEN = 512
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Capture:
|
|
21
|
+
"""Keeps the first HEAD_BYTES and last TAIL_BYTES of a stream, plus its total size."""
|
|
22
|
+
|
|
23
|
+
def __init__(self, head: int = HEAD_BYTES, tail: int = TAIL_BYTES):
|
|
24
|
+
self._head_n, self._tail_n = head, tail
|
|
25
|
+
self._head, self._tail = bytearray(), bytearray()
|
|
26
|
+
self.total = 0
|
|
27
|
+
|
|
28
|
+
def feed(self, chunk: bytes):
|
|
29
|
+
self.total += len(chunk)
|
|
30
|
+
if len(self._head) < self._head_n:
|
|
31
|
+
take = self._head_n - len(self._head)
|
|
32
|
+
self._head += chunk[:take]
|
|
33
|
+
chunk = chunk[take:]
|
|
34
|
+
if chunk:
|
|
35
|
+
self._tail += chunk
|
|
36
|
+
if len(self._tail) > self._tail_n:
|
|
37
|
+
del self._tail[: len(self._tail) - self._tail_n]
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def truncated(self) -> bool:
|
|
41
|
+
return self.total > len(self._head) + len(self._tail)
|
|
42
|
+
|
|
43
|
+
def text(self) -> str:
|
|
44
|
+
head = bytes(self._head).decode("utf-8", "replace")
|
|
45
|
+
tail = bytes(self._tail).decode("utf-8", "replace")
|
|
46
|
+
if not self.truncated:
|
|
47
|
+
return head + tail
|
|
48
|
+
# A cut can land inside a token. Drop the partial token at each cut so a
|
|
49
|
+
# secret is never left half-visible (redaction needs the whole token).
|
|
50
|
+
m = re.search(r"\S+\Z", head)
|
|
51
|
+
if m and len(m.group()) <= _MAX_CUT_TOKEN:
|
|
52
|
+
head = head[: m.start()]
|
|
53
|
+
m = re.match(r"\S+", tail)
|
|
54
|
+
if m and len(m.group()) <= _MAX_CUT_TOKEN:
|
|
55
|
+
tail = tail[m.end():]
|
|
56
|
+
omitted = self.total - len(self._head) - len(self._tail)
|
|
57
|
+
return f"{head}\n...[{omitted} bytes omitted]...\n{tail}"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def kill_tree(proc: subprocess.Popen):
|
|
61
|
+
"""SIGKILL the command's whole process group (it was started in its own session)."""
|
|
62
|
+
try:
|
|
63
|
+
if os.name == "posix":
|
|
64
|
+
os.killpg(proc.pid, signal.SIGKILL)
|
|
65
|
+
else:
|
|
66
|
+
proc.kill()
|
|
67
|
+
except OSError:
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def run_streaming(argv, cwd, timeout_s, max_bytes=DEFAULT_MAX_STREAM_BYTES, cleanup=None):
|
|
72
|
+
"""Run argv. Returns (exit_code, stdout_capture, stderr_capture, timed_out, output_exceeded).
|
|
73
|
+
|
|
74
|
+
On timeout, or when either stream passes max_bytes, the whole process group is
|
|
75
|
+
killed and cleanup() (if given) is called. Exit codes: 124 timeout, 137 output cap.
|
|
76
|
+
"""
|
|
77
|
+
out, err = Capture(), Capture()
|
|
78
|
+
kwargs = {"cwd": cwd, "stdin": subprocess.DEVNULL,
|
|
79
|
+
"stdout": subprocess.PIPE, "stderr": subprocess.PIPE}
|
|
80
|
+
if os.name == "posix":
|
|
81
|
+
kwargs["start_new_session"] = True
|
|
82
|
+
proc = subprocess.Popen(argv, **kwargs)
|
|
83
|
+
exceeded = threading.Event()
|
|
84
|
+
|
|
85
|
+
def pump(stream, cap):
|
|
86
|
+
try:
|
|
87
|
+
while True:
|
|
88
|
+
chunk = stream.read1(65536)
|
|
89
|
+
if not chunk:
|
|
90
|
+
return
|
|
91
|
+
cap.feed(chunk)
|
|
92
|
+
if cap.total > max_bytes:
|
|
93
|
+
exceeded.set()
|
|
94
|
+
return
|
|
95
|
+
except (OSError, ValueError):
|
|
96
|
+
return
|
|
97
|
+
|
|
98
|
+
threads = [threading.Thread(target=pump, args=(proc.stdout, out), daemon=True),
|
|
99
|
+
threading.Thread(target=pump, args=(proc.stderr, err), daemon=True)]
|
|
100
|
+
for t in threads:
|
|
101
|
+
t.start()
|
|
102
|
+
|
|
103
|
+
deadline = time.monotonic() + timeout_s
|
|
104
|
+
timed_out = False
|
|
105
|
+
while True:
|
|
106
|
+
if exceeded.is_set():
|
|
107
|
+
break
|
|
108
|
+
if proc.poll() is not None and not any(t.is_alive() for t in threads):
|
|
109
|
+
break
|
|
110
|
+
if time.monotonic() >= deadline:
|
|
111
|
+
timed_out = True
|
|
112
|
+
break
|
|
113
|
+
time.sleep(0.01)
|
|
114
|
+
|
|
115
|
+
if exceeded.is_set() or timed_out:
|
|
116
|
+
kill_tree(proc)
|
|
117
|
+
if cleanup:
|
|
118
|
+
cleanup()
|
|
119
|
+
proc.wait()
|
|
120
|
+
for t in threads:
|
|
121
|
+
t.join(timeout=2)
|
|
122
|
+
for s in (proc.stdout, proc.stderr):
|
|
123
|
+
try:
|
|
124
|
+
s.close()
|
|
125
|
+
except OSError:
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
code = proc.returncode
|
|
129
|
+
if timed_out:
|
|
130
|
+
code = 124
|
|
131
|
+
elif exceeded.is_set():
|
|
132
|
+
code = 137
|
|
133
|
+
return code, out, err, timed_out, exceeded.is_set()
|
agentbox/workspace.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Hash a workspace directory so the loop detector can tell whether files changed.
|
|
2
|
+
|
|
3
|
+
Properties: symlinks are recorded but never followed, special files (pipes, sockets)
|
|
4
|
+
are never opened, big files are hashed by size and mtime instead of content, and
|
|
5
|
+
unchanged files are not re-read between steps. Not thread-safe (module-level cache).
|
|
6
|
+
"""
|
|
7
|
+
import hashlib
|
|
8
|
+
import os
|
|
9
|
+
import stat
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
DEFAULT_IGNORE = frozenset({".git", "__pycache__", "node_modules", ".venv",
|
|
13
|
+
".pytest_cache", ".mypy_cache", ".ruff_cache"})
|
|
14
|
+
MAX_HASH_BYTES = 5 * 1024 * 1024 # larger files are hashed by size + mtime
|
|
15
|
+
_RACY_NS = 2_000_000_000 # trust a cached digest only for files older than 2 s
|
|
16
|
+
_CACHE: dict = {} # full path -> (size, mtime_ns, cached_at_ns, digest)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _sha_file(path: str):
|
|
20
|
+
h = hashlib.sha256()
|
|
21
|
+
try:
|
|
22
|
+
with open(path, "rb") as f:
|
|
23
|
+
for chunk in iter(lambda: f.read(65536), b""):
|
|
24
|
+
h.update(chunk)
|
|
25
|
+
except OSError:
|
|
26
|
+
return None
|
|
27
|
+
return h.hexdigest()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _entry(full: str, rel: str, max_file_bytes: int, use_cache: bool, now: int, seen: dict) -> bytes:
|
|
31
|
+
try:
|
|
32
|
+
st = os.lstat(full)
|
|
33
|
+
except OSError:
|
|
34
|
+
return f"X:{rel}\n".encode()
|
|
35
|
+
if stat.S_ISLNK(st.st_mode):
|
|
36
|
+
try:
|
|
37
|
+
target = os.readlink(full)
|
|
38
|
+
except OSError:
|
|
39
|
+
target = "?"
|
|
40
|
+
return f"L:{rel}:{target}\n".encode()
|
|
41
|
+
if not stat.S_ISREG(st.st_mode):
|
|
42
|
+
return f"S:{rel}\n".encode() # never open FIFOs, sockets or devices: open() could block
|
|
43
|
+
if st.st_size > max_file_bytes:
|
|
44
|
+
return f"B:{rel}:{st.st_size}:{st.st_mtime_ns}\n".encode()
|
|
45
|
+
cached = _CACHE.get(full) if use_cache else None
|
|
46
|
+
if (cached and cached[0] == st.st_size and cached[1] == st.st_mtime_ns
|
|
47
|
+
and cached[1] + _RACY_NS <= cached[2]):
|
|
48
|
+
entry = cached
|
|
49
|
+
else:
|
|
50
|
+
digest = _sha_file(full)
|
|
51
|
+
if digest is None:
|
|
52
|
+
return f"X:{rel}\n".encode()
|
|
53
|
+
entry = (st.st_size, st.st_mtime_ns, now, digest)
|
|
54
|
+
seen[full] = entry
|
|
55
|
+
return f"F:{rel}:{entry[3]}\n".encode()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def workspace_hash(root: str, ignore=None, max_file_bytes: int = MAX_HASH_BYTES,
|
|
59
|
+
use_cache: bool = True) -> str:
|
|
60
|
+
"""Short hash of the workspace. Changes when files are added, removed or modified."""
|
|
61
|
+
ignore = DEFAULT_IGNORE if ignore is None else frozenset(ignore)
|
|
62
|
+
root = os.path.abspath(root)
|
|
63
|
+
now = time.time_ns()
|
|
64
|
+
seen: dict = {}
|
|
65
|
+
h = hashlib.sha256()
|
|
66
|
+
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
|
|
67
|
+
dirnames[:] = sorted(d for d in dirnames if d not in ignore)
|
|
68
|
+
for d in list(dirnames): # symlinked directories are listed but never entered
|
|
69
|
+
full = os.path.join(dirpath, d)
|
|
70
|
+
if os.path.islink(full):
|
|
71
|
+
dirnames.remove(d)
|
|
72
|
+
rel = os.path.relpath(full, root).replace(os.sep, "/")
|
|
73
|
+
h.update(_entry(full, rel, max_file_bytes, use_cache, now, seen))
|
|
74
|
+
for name in sorted(filenames):
|
|
75
|
+
full = os.path.join(dirpath, name)
|
|
76
|
+
rel = os.path.relpath(full, root).replace(os.sep, "/")
|
|
77
|
+
h.update(_entry(full, rel, max_file_bytes, use_cache, now, seen))
|
|
78
|
+
if use_cache:
|
|
79
|
+
prefix = root + os.sep
|
|
80
|
+
for k in [k for k in _CACHE if k.startswith(prefix) and k not in seen]:
|
|
81
|
+
del _CACHE[k]
|
|
82
|
+
_CACHE.update(seen)
|
|
83
|
+
return h.hexdigest()[:16]
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentboxx
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A tamper-evident flight recorder for AI agents
|
|
5
|
+
Author-email: Prathmesh Kokate <prathmeshkokate775@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/Prathmesh010190/Agentboxx
|
|
8
|
+
Project-URL: Repository, https://github.com/Prathmesh010190/Agentboxx
|
|
9
|
+
Project-URL: Issues, https://github.com/Prathmesh010190/Agentboxx/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/Prathmesh010190/Agentboxx/tree/main/docs
|
|
11
|
+
Keywords: ai,agents,security,audit,flight-recorder,tamper-evident,sandbox
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: Software Development :: Testing
|
|
22
|
+
Classifier: Topic :: System :: Logging
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
29
|
+
Requires-Dist: ruff; extra == "dev"
|
|
30
|
+
Requires-Dist: jsonschema; extra == "dev"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
<div align="center">
|
|
34
|
+
|
|
35
|
+
# agentbox
|
|
36
|
+
|
|
37
|
+
**A tamper-evident flight recorder for AI agents.**
|
|
38
|
+
|
|
39
|
+
Run agent commands in an isolated sandbox, record what actually happened, sign every record, and catch agents that get stuck.
|
|
40
|
+
|
|
41
|
+
[](https://github.com/Prathmesh010190/Agentboxx/actions/workflows/ci.yml)
|
|
42
|
+
[](LICENSE)
|
|
43
|
+

|
|
44
|
+

|
|
45
|
+
|
|
46
|
+
</div>
|
|
47
|
+
|
|
48
|
+
> [!NOTE]
|
|
49
|
+
> **v0.2 MVP.** Working prototype validated end-to-end with sandboxed execution, cryptographic signing, loop detection, and live model execution. Interfaces may evolve before v1.0. Known gaps are listed under [Limitations](#limitations).
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Why agentbox
|
|
54
|
+
|
|
55
|
+
Most agent tooling records what the agent *says* it did, from inside the agent's own process. agentbox observes from the outside.
|
|
56
|
+
|
|
57
|
+
| | What it gives you |
|
|
58
|
+
|---|---|
|
|
59
|
+
| **Host-side capture** | Output, exit codes and timing are recorded by the host, not reported by the agent. |
|
|
60
|
+
| **Tamper-evident logs** | Every record is hash-chained and HMAC-signed with a key that never enters the sandbox. Edits, deletions and reordering are detected by `agentbox verify`. |
|
|
61
|
+
| **State-aware loop detection** | A repeated command only counts when the workspace did not change, which separates retrying from making progress. |
|
|
62
|
+
| **Hint feedback** | When the detector fires, a hint is added to the agent's next observation so it can change approach. |
|
|
63
|
+
| **Hardened sandbox** | Docker backend with no network, read-only root filesystem, dropped capabilities, non-root user and resource limits. |
|
|
64
|
+
|
|
65
|
+
## Architecture
|
|
66
|
+
|
|
67
|
+
<p align="center">
|
|
68
|
+
<img src="docs/architecture.svg" alt="agentbox architecture: model, Docker sandbox, recorder, signed log, and loop detector feeding hints back to the model" width="900">
|
|
69
|
+
</p>
|
|
70
|
+
|
|
71
|
+
| Module | Role |
|
|
72
|
+
|---|---|
|
|
73
|
+
| `signer.py` | Hash-chained, HMAC-signed event log and `verify_file` |
|
|
74
|
+
| `executor.py` | Local command runner, error fingerprints, workspace hashing |
|
|
75
|
+
| `docker_backend.py` | Runs commands in a locked-down container |
|
|
76
|
+
| `loop_detector.py` | Exact repeat, repeated error, no-progress and cycle detection |
|
|
77
|
+
| `recorder.py` | Main API: `Recorder.run(command)` |
|
|
78
|
+
| `agent.py` | Minimal ReAct-style loop that feeds detector hints back to the model |
|
|
79
|
+
|
|
80
|
+
## Quick start
|
|
81
|
+
|
|
82
|
+
Requires Python 3.10+. Docker is optional and used by the sandbox demos.
|
|
83
|
+
|
|
84
|
+
~~~bash
|
|
85
|
+
pip install agentboxx
|
|
86
|
+
~~~
|
|
87
|
+
|
|
88
|
+
Or install from source:
|
|
89
|
+
|
|
90
|
+
~~~bash
|
|
91
|
+
git clone https://github.com/Prathmesh010190/Agentboxx.git
|
|
92
|
+
cd Agentboxx
|
|
93
|
+
python3 -m venv .venv && source .venv/bin/activate
|
|
94
|
+
pip install -e ".[dev]"
|
|
95
|
+
pytest
|
|
96
|
+
~~~
|
|
97
|
+
|
|
98
|
+
Try it with one command (no Docker, no API key, nothing left behind):
|
|
99
|
+
|
|
100
|
+
~~~bash
|
|
101
|
+
agentbox demo
|
|
102
|
+
~~~
|
|
103
|
+
|
|
104
|
+
Run the demos from the repo:
|
|
105
|
+
|
|
106
|
+
~~~bash
|
|
107
|
+
python examples/demo_agent.py # stuck-agent demo (local backend)
|
|
108
|
+
python examples/demo_docker.py # shows what the Docker sandbox blocks
|
|
109
|
+
python examples/demo_agent_loop.py # full pipeline: detect, hint, recover
|
|
110
|
+
|
|
111
|
+
agentbox report runs/agent-demo.ndjson # readable summary
|
|
112
|
+
agentbox verify runs/agent-demo.ndjson # integrity check
|
|
113
|
+
agentbox list # every log in ./runs: session, start time, steps, verifies?
|
|
114
|
+
agentbox --version
|
|
115
|
+
~~~
|
|
116
|
+
|
|
117
|
+
`report` uses color for errors and warnings when writing to a terminal. Set `NO_COLOR=1` to turn it off. It supports `--format text|json|html` (default: `text`) and `--output <file>` (or `-o`) to export reports; HTML reports are completely self-contained with no external assets.
|
|
118
|
+
|
|
119
|
+
### Example: a stuck agent recovers
|
|
120
|
+
|
|
121
|
+
The scripted agent repeats a failing command. After the third identical failure on an unchanged workspace, the detector raises warnings and the next observation carries a hint:
|
|
122
|
+
|
|
123
|
+
~~~text
|
|
124
|
+
[2] Run it.
|
|
125
|
+
$ python3 main.py -> exit 1
|
|
126
|
+
[3] Run it.
|
|
127
|
+
$ python3 main.py -> exit 1
|
|
128
|
+
[4] Run it.
|
|
129
|
+
$ python3 main.py -> exit 1
|
|
130
|
+
!! warn: loop.exact
|
|
131
|
+
!! warn: loop.error_repeat
|
|
132
|
+
!! warn: loop.no_progress
|
|
133
|
+
[5] The hint is right: x = 0 causes the crash. Fix the value.
|
|
134
|
+
$ sed -i 's/x = 0/x = 2/' main.py -> exit 0
|
|
135
|
+
[6] Run it again.
|
|
136
|
+
$ python3 main.py -> exit 0
|
|
137
|
+
|
|
138
|
+
Result: {'reason': 'final_answer', 'final': 'Fixed the divide-by-zero; script prints 5.0.'}
|
|
139
|
+
~~~
|
|
140
|
+
|
|
141
|
+
### Example: tamper detection
|
|
142
|
+
|
|
143
|
+
Change any value in a log file and verification fails:
|
|
144
|
+
|
|
145
|
+
~~~text
|
|
146
|
+
$ agentbox verify runs/demo.ndjson
|
|
147
|
+
LOG HAS BEEN TAMPERED WITH OR IS CORRUPT:
|
|
148
|
+
- line 3: content was modified
|
|
149
|
+
~~~
|
|
150
|
+
|
|
151
|
+
## Use it in your own agent
|
|
152
|
+
|
|
153
|
+
~~~python
|
|
154
|
+
from agentbox import Recorder
|
|
155
|
+
from agentbox.docker_backend import DockerSandbox
|
|
156
|
+
|
|
157
|
+
sandbox = DockerSandbox("./workspace") # no network by default
|
|
158
|
+
sandbox.start()
|
|
159
|
+
rec = Recorder("my-session", workspace="./workspace", sandbox=sandbox)
|
|
160
|
+
|
|
161
|
+
result = rec.run("pytest -x", thought="Run the tests")
|
|
162
|
+
print(result["signals"]) # loop warnings, if any
|
|
163
|
+
|
|
164
|
+
rec.close("done") # also stops the container
|
|
165
|
+
~~~
|
|
166
|
+
|
|
167
|
+
`agentbox.agent.run_agent` wraps this in a ReAct-style loop. `ScriptedModel` needs no API key. `GeminiModel` connects to Google Gemini via standard-library REST (needs `GEMINI_API_KEY`, validated end-to-end). `AnthropicModel` is an optional adapter (`pip install anthropic`, set `ANTHROPIC_API_KEY`).
|
|
168
|
+
|
|
169
|
+
## What `agentbox verify` reports
|
|
170
|
+
|
|
171
|
+
| Result | Exit code | Meaning |
|
|
172
|
+
|---|---|---|
|
|
173
|
+
| `OK` | 0 | Chain, signatures and the closing manifest all match |
|
|
174
|
+
| `TAMPERED` | 1 | A record was edited, removed or reordered, or the log no longer matches its signed manifest |
|
|
175
|
+
| `INCOMPLETE` | 2 | The log is intact so far but was never closed: a crash, a running session, or a removed tail |
|
|
176
|
+
| *(error)* | 3 | The file is missing, empty, a directory or unreadable. This is not a verdict on the log, and `report` uses the same code |
|
|
177
|
+
|
|
178
|
+
The manifest (`<session>.manifest.json`) is written when a session closes and records the final record count and hash, signed with the same key. The verifier requires the manifest: a closed session whose manifest is missing is reported as `INCOMPLETE`, and a truncated log whose manifest is still present is reported as `TAMPERED`. Anyone who can read the signing key can forge both files, so copy manifests somewhere the agent host cannot write to if that matters. Public-key signing, as used for Nanda Town's evidence attestations, would let third parties verify logs without holding a secret, and is a possible future change.
|
|
179
|
+
|
|
180
|
+
## Detection signals
|
|
181
|
+
|
|
182
|
+
| Signal | Meaning |
|
|
183
|
+
|---|---|
|
|
184
|
+
| `loop.exact` | The same command was repeated on an unchanged workspace |
|
|
185
|
+
| `loop.error_repeat` | The same normalized error keeps recurring |
|
|
186
|
+
| `loop.no_progress` | Several steps failed while no files changed |
|
|
187
|
+
| `loop.cycle` | The agent alternates between the same few commands |
|
|
188
|
+
|
|
189
|
+
Warnings become hints for the model. Critical severity (for example five identical repeats) stops the run.
|
|
190
|
+
|
|
191
|
+
## Sandbox resource and crash events
|
|
192
|
+
|
|
193
|
+
With the Docker backend, the host records two extra signed events after each step: `sandbox.resource` (memory, CPU, process count) and `sandbox.crash` (an out-of-memory kill, or a container that stopped). Everything is read on the host through Docker, so the agent cannot influence it.
|
|
194
|
+
|
|
195
|
+
- Resource sampling adds about a second per step. Turn it off with `DockerSandbox(resource_stats=False)`.
|
|
196
|
+
- A sample is a snapshot after the step, not a peak.
|
|
197
|
+
- The local backend has no equivalent, and `agentbox report` does not show these events yet.
|
|
198
|
+
|
|
199
|
+
## Error signatures
|
|
200
|
+
|
|
201
|
+
Repeated-error detection compares normalized error signatures, so the same failure matches even when line numbers, file paths or runtime versions differ.
|
|
202
|
+
|
|
203
|
+
| Language | Recognized output | Signature class |
|
|
204
|
+
|---|---|---|
|
|
205
|
+
| Python | Tracebacks | Exception name, e.g. `ZeroDivisionError` |
|
|
206
|
+
| Node.js | Uncaught errors with stack frames | e.g. `TypeError`, `Error[ERR_MODULE_NOT_FOUND]` |
|
|
207
|
+
| Go | Panics, fatal errors, compile errors | `GoPanic`, `GoFatalError`, `GoCompileError` |
|
|
208
|
+
|
|
209
|
+
Anything else falls back to hashing the last lines of stderr. To add a language, see `src/agentbox/error_signatures.py`.
|
|
210
|
+
|
|
211
|
+
## Secret redaction
|
|
212
|
+
|
|
213
|
+
Commands, output and the model's reasoning are redacted **before** they are signed, because a signed record cannot be edited afterward. Recognized: private key blocks, AWS, GitHub, Slack and Google keys, `sk-` style API keys, JWTs, `Authorization` headers, passwords in URLs, and `NAME=value` where the name contains `password`, `secret`, `token` or `api_key`. Each step records only a count per pattern, never the value.
|
|
214
|
+
|
|
215
|
+
Limits to know about:
|
|
216
|
+
- Patterns are heuristics. They miss some secrets (for example a secret with no recognizable name or format) and sometimes hide harmless values such as a long `*_token=` setting.
|
|
217
|
+
- The command itself still runs with the real value. Only the **record** is redacted.
|
|
218
|
+
- With the agent loop, the model also receives the redacted output, so it cannot read a secret that a command printed.
|
|
219
|
+
- Files in the workspace are not logged, but anything a command prints from them is covered by these patterns only.
|
|
220
|
+
- Add your own patterns with `agentbox.redact.add_pattern(name, regex)`.
|
|
221
|
+
|
|
222
|
+
## Log format
|
|
223
|
+
|
|
224
|
+
The log format is documented in [docs/log-format.md](docs/log-format.md) and described by JSON Schemas ([record](docs/schema.json), [manifest](docs/manifest.schema.json)). [examples/verify_standalone.py](examples/verify_standalone.py) verifies a log using only the Python standard library, as an independent check that the spec is complete.
|
|
225
|
+
|
|
226
|
+
## Threat model
|
|
227
|
+
|
|
228
|
+
- **Docker backend:** commands run as a non-root user in a container with no network, a read-only root filesystem, all Linux capabilities dropped, `no-new-privileges`, and CPU, memory and process limits. The signing key and logs stay on the host and are never mounted into the container.
|
|
229
|
+
- **Local backend:** no isolation. The agent runs as your user and could read the signing key. Use it only for trusted experiments.
|
|
230
|
+
- Docker isolation is strong but not a perfect boundary. Run untrusted agents in a disposable VM.
|
|
231
|
+
- The log shows that records were not altered after writing. It does not prove the agent's intent, and it cannot see anything that bypasses the executor.
|
|
232
|
+
|
|
233
|
+
## Limitations
|
|
234
|
+
|
|
235
|
+
- Windows is experimental. The Docker backend no longer needs Unix user IDs and runs as user `1000:1000` in the container (override with `DockerSandbox(user=...)`), but it has not been run on real Windows hardware yet. It needs Docker Desktop with Linux containers. The local backend still needs `bash` (Git Bash), and on Windows a timeout kills only the direct child process.
|
|
236
|
+
- Detector thresholds are initial values, not calibrated on real agent traces.
|
|
237
|
+
- No network capture yet (the sandbox simply has no network).
|
|
238
|
+
- No semantic drift detection yet; detection is command and state based.
|
|
239
|
+
- Error signatures cover Python, Node.js and Go only. Other languages use a generic fallback.
|
|
240
|
+
|
|
241
|
+
## Roadmap
|
|
242
|
+
|
|
243
|
+
- [x] Signed, hash-chained event log with verification
|
|
244
|
+
- [x] Docker sandbox backend
|
|
245
|
+
- [x] Loop detection with hint feedback
|
|
246
|
+
- [x] ReAct-style agent loop
|
|
247
|
+
- [x] Stable error signatures for Python, Node.js and Go
|
|
248
|
+
- [x] Truncation detection with a signed closing manifest
|
|
249
|
+
- [x] Secret redaction before signing
|
|
250
|
+
- [ ] Output size cap and process-tree kill on timeout
|
|
251
|
+
- [x] Sandbox resource and crash events (Docker backend)
|
|
252
|
+
- [ ] Public-key signing so third parties can verify logs
|
|
253
|
+
- [x] Validate against a live model ([#5](https://github.com/Prathmesh010190/Agentboxx/issues/5))
|
|
254
|
+
- [ ] Network egress capture (design in [docs/network-egress.md](docs/network-egress.md))
|
|
255
|
+
- [ ] Semantic drift detector
|
|
256
|
+
- [ ] Session replay UI
|
|
257
|
+
- [ ] Threshold calibration on recorded sessions
|
|
258
|
+
|
|
259
|
+
## Contributing
|
|
260
|
+
|
|
261
|
+
Contributions are welcome. Start with [CONTRIBUTING.md](CONTRIBUTING.md) and the issues labeled [`good first issue`](https://github.com/Prathmesh010190/Agentboxx/labels/good%20first%20issue).
|
|
262
|
+
|
|
263
|
+
## License
|
|
264
|
+
|
|
265
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
agentbox/__init__.py,sha256=dYEww15gZh1FGuOR9ztShNQ06rhaEL-qcrwFDQUNreA,220
|
|
2
|
+
agentbox/agent.py,sha256=XhtzisW1i7BSxmwgjpBWbrwP40onT2yQzRrNSq8g5MU,5646
|
|
3
|
+
agentbox/cli.py,sha256=kOEN-zHD1-kbYXING7epS5H28oSoO_mflQAnVVITd_Q,9433
|
|
4
|
+
agentbox/docker_backend.py,sha256=vzfajjygGtUdBMZlE8-s9W_y-_ljix8-7I4fKV0-WpY,7271
|
|
5
|
+
agentbox/error_signatures.py,sha256=Tk1N9L6i6lMuRySPxuqorB3Fr4sgNZwwIJF-M6bsdn4,1638
|
|
6
|
+
agentbox/executor.py,sha256=X32Mr2hfW_DtHWP0u-jV-bPDY5J0Q_PLgpwcH6nIu80,3600
|
|
7
|
+
agentbox/loop_detector.py,sha256=OzdtDw6vI-iDVqMH_zv2wkAtrJ2aPGx2mvGDx7y6Nj0,2742
|
|
8
|
+
agentbox/recorder.py,sha256=tGs_LmaOpjfaIzdL_uRnKY6WO7ZQcSji0RJHXqWmNpI,3437
|
|
9
|
+
agentbox/redact.py,sha256=gil6859clHOAEIaTkdxtnY15AzJz83uUQ2bbxNWd0hI,2925
|
|
10
|
+
agentbox/report.py,sha256=rWz2Sf6TR0iWub7WXX7TRbm5mobEF3dNAcsQbUvW3z8,12063
|
|
11
|
+
agentbox/sandbox_stats.py,sha256=dsOPcbm1ZwpWqdANdPval4WPCaq3lu2YSQzuwFBkBp0,2423
|
|
12
|
+
agentbox/signer.py,sha256=3DVrblKCVnf6FL8tbGHJ4Y02lC1YtJ345OaqFN9ZYC0,6041
|
|
13
|
+
agentbox/streaming.py,sha256=yMzwH_jS_Fq0gb9MOHPZBV3zhBoR45KD80QUFKY_kVY,4406
|
|
14
|
+
agentbox/workspace.py,sha256=Bw7kPC-lRtREAYbzE7TYlm-xVbEDesAlGTltuVMomsE,3470
|
|
15
|
+
agentboxx-0.2.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
16
|
+
agentboxx-0.2.1.dist-info/METADATA,sha256=nbRZNqzAVqdEeHZIswtdCfxcXv31yji93RwihSl4HiQ,13323
|
|
17
|
+
agentboxx-0.2.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
18
|
+
agentboxx-0.2.1.dist-info/entry_points.txt,sha256=u_320LVs7Vndr2ti0gFQXhR8o9kkjW0LmNjLzEXLr50,47
|
|
19
|
+
agentboxx-0.2.1.dist-info/top_level.txt,sha256=lNpen_9W4iU-XQIGpizrWb3jS7tj6k27fUBWT4u0HEQ,9
|
|
20
|
+
agentboxx-0.2.1.dist-info/RECORD,,
|