Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 88f58bf4c0 | |||
| ca0dc72b89 | |||
| 2fc99ea098 | |||
| 9a9235f2af | |||
| 42f79283f0 | |||
| d6b9d7af3e |
@@ -0,0 +1,9 @@
|
|||||||
|
[run]
|
||||||
|
branch = True
|
||||||
|
source = .
|
||||||
|
|
||||||
|
[report]
|
||||||
|
omit =
|
||||||
|
bot_bottle/egress_addon.py
|
||||||
|
bot_bottle/cli/tui.py
|
||||||
|
tests/*
|
||||||
@@ -39,8 +39,14 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
python-version: "3.12"
|
python-version: "3.12"
|
||||||
|
|
||||||
|
- name: Install dev requirements
|
||||||
|
run: python3 -m pip install -r requirements-dev.txt
|
||||||
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: python3 -m unittest discover -t . -s tests/unit -v
|
run: python3 -m coverage run -m unittest discover -t . -s tests/unit -v
|
||||||
|
|
||||||
|
- name: Report unit coverage
|
||||||
|
run: python3 -m coverage report -m
|
||||||
|
|
||||||
integration:
|
integration:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
@@ -22,3 +22,4 @@ venv/
|
|||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
.coverage
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
act on them (approve / modify / reject).
|
act on them (approve / modify / reject).
|
||||||
|
|
||||||
Curses-based TUI; modify-then-approve shells out to $EDITOR. The
|
Curses-based TUI; modify-then-approve shells out to $EDITOR. The
|
||||||
approval handler wires to PRD 0016 (capability-block), which rebuilds
|
Egress proposals are queued for operator review as full routes.yaml
|
||||||
the bottle Dockerfile. Egress proposals are queued for operator review
|
updates.
|
||||||
as full routes.yaml updates.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -22,10 +21,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
from .. import supervise as _supervise
|
from .. import supervise as _supervise
|
||||||
from ..bottle_state import read_metadata
|
from ..bottle_state import read_metadata
|
||||||
# from ..backend.docker.capability_apply import (
|
|
||||||
# CapabilityApplyError,
|
|
||||||
# apply_capability_change,
|
|
||||||
# )
|
|
||||||
from ..backend.docker.egress_apply import (
|
from ..backend.docker.egress_apply import (
|
||||||
EgressApplyError,
|
EgressApplyError,
|
||||||
applicator as _docker_applicator,
|
applicator as _docker_applicator,
|
||||||
@@ -38,10 +33,6 @@ from ..backend.smolmachines.egress_apply import (
|
|||||||
)
|
)
|
||||||
from ..log import Die, error, info
|
from ..log import Die, error, info
|
||||||
|
|
||||||
|
|
||||||
class CapabilityApplyError(RuntimeError):
|
|
||||||
"""Placeholder while capability_apply is disabled."""
|
|
||||||
|
|
||||||
from ..supervise import (
|
from ..supervise import (
|
||||||
COMPONENT_FOR_TOOL,
|
COMPONENT_FOR_TOOL,
|
||||||
AuditEntry,
|
AuditEntry,
|
||||||
@@ -50,12 +41,10 @@ from ..supervise import (
|
|||||||
STATUS_APPROVED,
|
STATUS_APPROVED,
|
||||||
STATUS_MODIFIED,
|
STATUS_MODIFIED,
|
||||||
STATUS_REJECTED,
|
STATUS_REJECTED,
|
||||||
TOOL_CAPABILITY_BLOCK,
|
|
||||||
TOOL_EGRESS_ALLOW,
|
TOOL_EGRESS_ALLOW,
|
||||||
TOOL_EGRESS_BLOCK,
|
TOOL_EGRESS_BLOCK,
|
||||||
TOOL_GITLEAKS_ALLOW,
|
TOOL_GITLEAKS_ALLOW,
|
||||||
TOOL_EGRESS_TOKEN_ALLOW,
|
TOOL_EGRESS_TOKEN_ALLOW,
|
||||||
archive_proposal,
|
|
||||||
list_pending_proposals,
|
list_pending_proposals,
|
||||||
render_diff,
|
render_diff,
|
||||||
write_audit_entry,
|
write_audit_entry,
|
||||||
@@ -83,7 +72,7 @@ class QueuedProposal:
|
|||||||
# Errors any remediation engine may raise. Caught by the TUI key
|
# Errors any remediation engine may raise. Caught by the TUI key
|
||||||
# handlers and surfaced in the status line so a failed apply keeps
|
# handlers and surfaced in the status line so a failed apply keeps
|
||||||
# the proposal pending rather than crashing curses.
|
# the proposal pending rather than crashing curses.
|
||||||
ApplyError = (CapabilityApplyError, EgressApplyError)
|
ApplyError = (EgressApplyError,)
|
||||||
|
|
||||||
|
|
||||||
def apply_routes_change(slug: str, content: str) -> tuple[str, str]:
|
def apply_routes_change(slug: str, content: str) -> tuple[str, str]:
|
||||||
@@ -143,8 +132,6 @@ def _detail_lines(
|
|||||||
|
|
||||||
|
|
||||||
def _suffix_for_tool(tool: str) -> str:
|
def _suffix_for_tool(tool: str) -> str:
|
||||||
if tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
return ".dockerfile"
|
|
||||||
if tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
if tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
||||||
return ".yaml"
|
return ".yaml"
|
||||||
if tool in (TOOL_GITLEAKS_ALLOW, TOOL_EGRESS_TOKEN_ALLOW):
|
if tool in (TOOL_GITLEAKS_ALLOW, TOOL_EGRESS_TOKEN_ALLOW):
|
||||||
@@ -166,17 +153,6 @@ def approve(
|
|||||||
file_to_apply = final_file if final_file is not None else qp.proposal.proposed_file
|
file_to_apply = final_file if final_file is not None else qp.proposal.proposed_file
|
||||||
|
|
||||||
diff_before, diff_after = "", ""
|
diff_before, diff_after = "", ""
|
||||||
# if qp.proposal.tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
# _meta = read_metadata(qp.proposal.bottle_slug)
|
|
||||||
# if _meta is not None and not _meta.compose_project:
|
|
||||||
# raise CapabilityApplyError(
|
|
||||||
# "capability-block remediation is not supported for smolmachines "
|
|
||||||
# "bottles. Reject this proposal or handle the capability change "
|
|
||||||
# "manually, then restart the bottle."
|
|
||||||
# )
|
|
||||||
# diff_before, diff_after = apply_capability_change(
|
|
||||||
# qp.proposal.bottle_slug, file_to_apply,
|
|
||||||
# )
|
|
||||||
if qp.proposal.tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
if qp.proposal.tool in (TOOL_EGRESS_ALLOW, TOOL_EGRESS_BLOCK):
|
||||||
diff_before, diff_after = apply_routes_change(
|
diff_before, diff_after = apply_routes_change(
|
||||||
qp.proposal.bottle_slug,
|
qp.proposal.bottle_slug,
|
||||||
@@ -194,9 +170,6 @@ def approve(
|
|||||||
qp, action=status, notes=notes,
|
qp, action=status, notes=notes,
|
||||||
diff_before=diff_before, diff_after=diff_after,
|
diff_before=diff_before, diff_after=diff_after,
|
||||||
)
|
)
|
||||||
if qp.proposal.tool == TOOL_CAPABILITY_BLOCK:
|
|
||||||
archive_proposal(qp.queue_dir, qp.proposal.id)
|
|
||||||
|
|
||||||
|
|
||||||
def reject(qp: QueuedProposal, *, reason: str) -> None:
|
def reject(qp: QueuedProposal, *, reason: str) -> None:
|
||||||
"""Write a rejection response and an audit entry."""
|
"""Write a rejection response and an audit entry."""
|
||||||
@@ -346,7 +319,7 @@ def _list_once() -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _try_init_green() -> int:
|
def _try_init_green() -> int: # pragma: no cover
|
||||||
"""Initialise a green color pair and return its attr, or 0."""
|
"""Initialise a green color pair and return its attr, or 0."""
|
||||||
try:
|
try:
|
||||||
curses.start_color()
|
curses.start_color()
|
||||||
@@ -357,7 +330,7 @@ def _try_init_green() -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _main_loop(stdscr: "curses._CursesWindow") -> None: # type: ignore
|
def _main_loop(stdscr: "curses._CursesWindow") -> None: # type: ignore # pragma: no cover
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
stdscr.timeout(_REFRESH_INTERVAL_MS)
|
stdscr.timeout(_REFRESH_INTERVAL_MS)
|
||||||
green_attr = _try_init_green()
|
green_attr = _try_init_green()
|
||||||
@@ -447,7 +420,7 @@ def _render(
|
|||||||
status_line: str,
|
status_line: str,
|
||||||
*,
|
*,
|
||||||
green_attr: int = 0, # noqa: F841 — unused, but required by interface
|
green_attr: int = 0, # noqa: F841 — unused, but required by interface
|
||||||
) -> None:
|
) -> None: # pragma: no cover
|
||||||
stdscr.erase()
|
stdscr.erase()
|
||||||
h, w = stdscr.getmaxyx()
|
h, w = stdscr.getmaxyx()
|
||||||
header = f"bot-bottle supervise ({len(pending)} pending)"
|
header = f"bot-bottle supervise ({len(pending)} pending)"
|
||||||
@@ -498,7 +471,7 @@ def _detail_view(
|
|||||||
qp: QueuedProposal,
|
qp: QueuedProposal,
|
||||||
*,
|
*,
|
||||||
green_attr: int = 0,
|
green_attr: int = 0,
|
||||||
) -> None:
|
) -> None: # pragma: no cover
|
||||||
"""Render the full proposal. Scrollable. Press q to return."""
|
"""Render the full proposal. Scrollable. Press q to return."""
|
||||||
lines = _detail_lines(qp, green_attr=green_attr)
|
lines = _detail_lines(qp, green_attr=green_attr)
|
||||||
offset = 0
|
offset = 0
|
||||||
@@ -550,7 +523,7 @@ def _detail_view(
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None: # type: ignore
|
def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None: # type: ignore # pragma: no cover
|
||||||
"""Suspend curses, open $EDITOR on the proposed file, return edited content."""
|
"""Suspend curses, open $EDITOR on the proposed file, return edited content."""
|
||||||
suffix = _suffix_for_tool(qp.proposal.tool)
|
suffix = _suffix_for_tool(qp.proposal.tool)
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
@@ -561,7 +534,7 @@ def _modify(stdscr: "curses._CursesWindow", qp: QueuedProposal) -> str | None:
|
|||||||
return edited
|
return edited
|
||||||
|
|
||||||
|
|
||||||
def _prompt(stdscr: "curses._CursesWindow", label: str) -> str: # type: ignore
|
def _prompt(stdscr: "curses._CursesWindow", label: str) -> str: # type: ignore # pragma: no cover
|
||||||
"""One-line input at the bottom of the screen."""
|
"""One-line input at the bottom of the screen."""
|
||||||
curses.curs_set(1)
|
curses.curs_set(1)
|
||||||
h, _ = stdscr.getmaxyx()
|
h, _ = stdscr.getmaxyx()
|
||||||
|
|||||||
@@ -148,13 +148,7 @@ class GitHttpHandler(BaseHTTPRequestHandler):
|
|||||||
key, _, value = line.decode("latin1").partition(":")
|
key, _, value = line.decode("latin1").partition(":")
|
||||||
value = value.strip()
|
value = value.strip()
|
||||||
if key.lower() == "status":
|
if key.lower() == "status":
|
||||||
try:
|
status = int(value.split()[0])
|
||||||
status = int(value.split()[0])
|
|
||||||
except (ValueError, IndexError):
|
|
||||||
self.log_message(
|
|
||||||
"malformed CGI Status header %r; using 500", value,
|
|
||||||
)
|
|
||||||
status = 500
|
|
||||||
else:
|
else:
|
||||||
headers.append((key, value))
|
headers.append((key, value))
|
||||||
self.send_response(status)
|
self.send_response(status)
|
||||||
|
|||||||
@@ -90,19 +90,19 @@ def parse_jsonrpc(body: bytes) -> JsonRpcRequest:
|
|||||||
try:
|
try:
|
||||||
raw = json.loads(body)
|
raw = json.loads(body)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
raise _RpcClientError(ERR_PARSE, f"parse error: {e}") from e
|
raise _RpcError(ERR_PARSE, f"parse error: {e}") from e
|
||||||
if not isinstance(raw, dict):
|
if not isinstance(raw, dict):
|
||||||
raise _RpcClientError(ERR_INVALID_REQUEST, "request must be a JSON object")
|
raise _RpcError(ERR_INVALID_REQUEST, "request must be a JSON object")
|
||||||
if raw.get("jsonrpc") != JSONRPC_VERSION:
|
if raw.get("jsonrpc") != JSONRPC_VERSION:
|
||||||
raise _RpcClientError(ERR_INVALID_REQUEST, "jsonrpc field must be '2.0'")
|
raise _RpcError(ERR_INVALID_REQUEST, "jsonrpc field must be '2.0'")
|
||||||
method = raw.get("method")
|
method = raw.get("method")
|
||||||
if not isinstance(method, str):
|
if not isinstance(method, str):
|
||||||
raise _RpcClientError(ERR_INVALID_REQUEST, "method must be a string")
|
raise _RpcError(ERR_INVALID_REQUEST, "method must be a string")
|
||||||
params = raw.get("params", {})
|
params = raw.get("params", {})
|
||||||
if params is None:
|
if params is None:
|
||||||
params = {}
|
params = {}
|
||||||
if not isinstance(params, dict):
|
if not isinstance(params, dict):
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, "params must be an object")
|
raise _RpcError(ERR_INVALID_PARAMS, "params must be an object")
|
||||||
rpc_id = raw.get("id", _NO_ID)
|
rpc_id = raw.get("id", _NO_ID)
|
||||||
is_notification = rpc_id is _NO_ID
|
is_notification = rpc_id is _NO_ID
|
||||||
return JsonRpcRequest(
|
return JsonRpcRequest(
|
||||||
@@ -117,23 +117,12 @@ _NO_ID = object()
|
|||||||
|
|
||||||
|
|
||||||
class _RpcError(Exception):
|
class _RpcError(Exception):
|
||||||
"""Base class for all typed RPC errors that surface as JSON-RPC error responses."""
|
|
||||||
def __init__(self, code: int, message: str):
|
def __init__(self, code: int, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
self.code = code
|
self.code = code
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
class _RpcClientError(_RpcError):
|
|
||||||
"""Caller sent a bad request; returned verbatim, no server-side logging."""
|
|
||||||
|
|
||||||
|
|
||||||
class _RpcInternalError(_RpcError):
|
|
||||||
"""Server-side fault; logged at ERROR with cause, always returns ERR_INTERNAL."""
|
|
||||||
def __init__(self, message: str) -> None:
|
|
||||||
super().__init__(ERR_INTERNAL, message)
|
|
||||||
|
|
||||||
|
|
||||||
def jsonrpc_result(request_id: object, result: object) -> bytes:
|
def jsonrpc_result(request_id: object, result: object) -> bytes:
|
||||||
payload = {"jsonrpc": JSONRPC_VERSION, "id": request_id, "result": result}
|
payload = {"jsonrpc": JSONRPC_VERSION, "id": request_id, "result": result}
|
||||||
return (json.dumps(payload) + "\n").encode("utf-8")
|
return (json.dumps(payload) + "\n").encode("utf-8")
|
||||||
@@ -301,7 +290,7 @@ def validate_proposed_file(tool: str, content: str) -> None:
|
|||||||
catches obvious paste-errors / wrong-tool selections before they
|
catches obvious paste-errors / wrong-tool selections before they
|
||||||
enter the queue."""
|
enter the queue."""
|
||||||
if not content.strip():
|
if not content.strip():
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, f"{tool}: proposed file is empty")
|
raise _RpcError(ERR_INVALID_PARAMS, f"{tool}: proposed file is empty")
|
||||||
if tool == _sv.TOOL_CAPABILITY_BLOCK:
|
if tool == _sv.TOOL_CAPABILITY_BLOCK:
|
||||||
# Dockerfiles are too varied to validate syntactically beyond
|
# Dockerfiles are too varied to validate syntactically beyond
|
||||||
# non-empty. The operator reads the diff in the TUI.
|
# non-empty. The operator reads the diff in the TUI.
|
||||||
@@ -310,17 +299,17 @@ def validate_proposed_file(tool: str, content: str) -> None:
|
|||||||
try:
|
try:
|
||||||
config = load_config(content)
|
config = load_config(content)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise _RpcClientError(
|
raise _RpcError(
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
f"{tool}: proposed routes.yaml is not valid: {e}",
|
f"{tool}: proposed routes.yaml is not valid: {e}",
|
||||||
) from e
|
) from e
|
||||||
if config.log != LOG_OFF:
|
if config.log != LOG_OFF:
|
||||||
raise _RpcClientError(
|
raise _RpcError(
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
f"{tool}: proposed routes.yaml must not change egress logging",
|
f"{tool}: proposed routes.yaml must not change egress logging",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, f"unknown tool {tool!r}")
|
raise _RpcError(ERR_INVALID_PARAMS, f"unknown tool {tool!r}")
|
||||||
|
|
||||||
|
|
||||||
# --- MCP handlers ----------------------------------------------------------
|
# --- MCP handlers ----------------------------------------------------------
|
||||||
@@ -393,17 +382,17 @@ def handle_tools_call(
|
|||||||
doesn't need operator approval."""
|
doesn't need operator approval."""
|
||||||
name = params.get("name")
|
name = params.get("name")
|
||||||
if not isinstance(name, str):
|
if not isinstance(name, str):
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, "tools/call missing 'name'")
|
raise _RpcError(ERR_INVALID_PARAMS, "tools/call missing 'name'")
|
||||||
if name == _sv.TOOL_LIST_EGRESS_ROUTES:
|
if name == _sv.TOOL_LIST_EGRESS_ROUTES:
|
||||||
return handle_list_egress_routes(typing.cast(dict[str, object], params.get("arguments", {})), config)
|
return handle_list_egress_routes(typing.cast(dict[str, object], params.get("arguments", {})), config)
|
||||||
|
|
||||||
args_raw = params.get("arguments", {})
|
args_raw = params.get("arguments", {})
|
||||||
if not isinstance(args_raw, dict):
|
if not isinstance(args_raw, dict):
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, "tools/call 'arguments' must be an object")
|
raise _RpcError(ERR_INVALID_PARAMS, "tools/call 'arguments' must be an object")
|
||||||
|
|
||||||
justification = args_raw.get("justification")
|
justification = args_raw.get("justification")
|
||||||
if not isinstance(justification, str) or not justification.strip():
|
if not isinstance(justification, str) or not justification.strip():
|
||||||
raise _RpcClientError(
|
raise _RpcError(
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
f"{name}: 'justification' is required and must be a non-empty string",
|
f"{name}: 'justification' is required and must be a non-empty string",
|
||||||
)
|
)
|
||||||
@@ -412,13 +401,13 @@ def handle_tools_call(
|
|||||||
file_field = PROPOSED_FILE_FIELD[name]
|
file_field = PROPOSED_FILE_FIELD[name]
|
||||||
proposed_file = args_raw.get(file_field)
|
proposed_file = args_raw.get(file_field)
|
||||||
if not isinstance(proposed_file, str):
|
if not isinstance(proposed_file, str):
|
||||||
raise _RpcClientError(
|
raise _RpcError(
|
||||||
ERR_INVALID_PARAMS,
|
ERR_INVALID_PARAMS,
|
||||||
f"{name}: '{file_field}' is required and must be a string",
|
f"{name}: '{file_field}' is required and must be a string",
|
||||||
)
|
)
|
||||||
validate_proposed_file(name, proposed_file)
|
validate_proposed_file(name, proposed_file)
|
||||||
else:
|
else:
|
||||||
raise _RpcClientError(ERR_INVALID_PARAMS, f"unknown tool {name!r}")
|
raise _RpcError(ERR_INVALID_PARAMS, f"unknown tool {name!r}")
|
||||||
|
|
||||||
proposal = _sv.Proposal.new(
|
proposal = _sv.Proposal.new(
|
||||||
bottle_slug=config.bottle_slug,
|
bottle_slug=config.bottle_slug,
|
||||||
@@ -427,10 +416,7 @@ def handle_tools_call(
|
|||||||
justification=justification,
|
justification=justification,
|
||||||
current_file_hash=_sv.sha256_hex(proposed_file),
|
current_file_hash=_sv.sha256_hex(proposed_file),
|
||||||
)
|
)
|
||||||
try:
|
_sv.write_proposal(config.queue_dir, proposal)
|
||||||
_sv.write_proposal(config.queue_dir, proposal)
|
|
||||||
except OSError as e:
|
|
||||||
raise _RpcInternalError(f"failed to write proposal to queue: {e}") from e
|
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
f"supervise: queued proposal {proposal.id} ({name}) "
|
f"supervise: queued proposal {proposal.id} ({name}) "
|
||||||
f"for bottle {config.bottle_slug}; waiting for operator...\n"
|
f"for bottle {config.bottle_slug}; waiting for operator...\n"
|
||||||
@@ -450,10 +436,7 @@ def handle_tools_call(
|
|||||||
"content": [{"type": "text", "text": text}],
|
"content": [{"type": "text", "text": text}],
|
||||||
"isError": False,
|
"isError": False,
|
||||||
}
|
}
|
||||||
try:
|
_sv.archive_proposal(config.queue_dir, proposal.id)
|
||||||
_sv.archive_proposal(config.queue_dir, proposal.id)
|
|
||||||
except OSError as e:
|
|
||||||
raise _RpcInternalError(f"failed to archive proposal: {e}") from e
|
|
||||||
|
|
||||||
text = format_response_text(response)
|
text = format_response_text(response)
|
||||||
return {
|
return {
|
||||||
@@ -529,7 +512,7 @@ class MCPHandler(http.server.BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
req = parse_jsonrpc(body)
|
req = parse_jsonrpc(body)
|
||||||
except _RpcClientError as e:
|
except _RpcError as e:
|
||||||
self._write_jsonrpc(jsonrpc_error(None, e.code, e.message))
|
self._write_jsonrpc(jsonrpc_error(None, e.code, e.message))
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -537,19 +520,11 @@ class MCPHandler(http.server.BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
result = self._dispatch(req, config)
|
result = self._dispatch(req, config)
|
||||||
except _RpcClientError as e:
|
except _RpcError as e:
|
||||||
self._write_jsonrpc(jsonrpc_error(req.id, e.code, e.message))
|
self._write_jsonrpc(jsonrpc_error(req.id, e.code, e.message))
|
||||||
return
|
return
|
||||||
except _RpcInternalError as e:
|
except Exception as e: # noqa: W0718 — catch-all for RPC dispatch errors
|
||||||
cause = e.__cause__
|
sys.stderr.write(f"supervise: internal error: {e}\n")
|
||||||
detail = f": {cause}" if cause else ""
|
|
||||||
sys.stderr.write(f"supervise: internal error: {e.message}{detail}\n")
|
|
||||||
sys.stderr.flush()
|
|
||||||
self._write_jsonrpc(jsonrpc_error(req.id, ERR_INTERNAL, "internal error"))
|
|
||||||
return
|
|
||||||
except Exception as e: # noqa: W0718 — unexpected errors
|
|
||||||
sys.stderr.write(f"supervise: unexpected error: {type(e).__name__}: {e}\n")
|
|
||||||
sys.stderr.flush()
|
|
||||||
self._write_jsonrpc(jsonrpc_error(req.id, ERR_INTERNAL, "internal error"))
|
self._write_jsonrpc(jsonrpc_error(req.id, ERR_INTERNAL, "internal error"))
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -568,7 +543,7 @@ class MCPHandler(http.server.BaseHTTPRequestHandler):
|
|||||||
return handle_tools_list(req.params)
|
return handle_tools_list(req.params)
|
||||||
if method == "tools/call":
|
if method == "tools/call":
|
||||||
return handle_tools_call(req.params, config)
|
return handle_tools_call(req.params, config)
|
||||||
raise _RpcClientError(ERR_METHOD_NOT_FOUND, f"method not found: {method}")
|
raise _RpcError(ERR_METHOD_NOT_FOUND, f"method not found: {method}")
|
||||||
|
|
||||||
def _write_jsonrpc(self, body: bytes) -> None:
|
def _write_jsonrpc(self, body: bytes) -> None:
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
|
|||||||
@@ -4,3 +4,4 @@
|
|||||||
|
|
||||||
pylint>=3.0.0
|
pylint>=3.0.0
|
||||||
pyright>=1.1.300
|
pyright>=1.1.300
|
||||||
|
coverage>=7.0.0
|
||||||
|
|||||||
@@ -92,9 +92,9 @@ class TestSandboxEscape(unittest.TestCase):
|
|||||||
"on PATH: curl -sSL https://smolmachines.com/install.sh | sh"
|
"on PATH: curl -sSL https://smolmachines.com/install.sh | sh"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Throwaway "identity file" for the git-gate's `identity` field.
|
# Throwaway static key for the git-gate fixture. It need not
|
||||||
# It need not be a real SSH key: test 5 reaches gitleaks before
|
# be a real SSH key: test 5 reaches gitleaks before any SSH
|
||||||
# any SSH attempt anyway.
|
# attempt anyway.
|
||||||
fd, kp = tempfile.mkstemp(prefix="sandbox-test-key.")
|
fd, kp = tempfile.mkstemp(prefix="sandbox-test-key.")
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
cls._key_path = Path(kp)
|
cls._key_path = Path(kp)
|
||||||
@@ -123,7 +123,10 @@ class TestSandboxEscape(unittest.TestCase):
|
|||||||
"git-gate": {"repos": {
|
"git-gate": {"repos": {
|
||||||
"throwaway": {
|
"throwaway": {
|
||||||
"url": "ssh://git@unreachable.invalid:22/throwaway.git",
|
"url": "ssh://git@unreachable.invalid:22/throwaway.git",
|
||||||
"identity": str(cls._key_path),
|
"key": {
|
||||||
|
"provider": "static",
|
||||||
|
"path": str(cls._key_path),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ class TestSmolmachinesLaunch(unittest.TestCase):
|
|||||||
# connect fails, which is the property chunk 3 will
|
# connect fails, which is the property chunk 3 will
|
||||||
# preserve once egress is actually running.
|
# preserve once egress is actually running.
|
||||||
r = self.bottle.exec(
|
r = self.bottle.exec(
|
||||||
|
"env -u HTTPS_PROXY -u HTTP_PROXY -u https_proxy -u http_proxy "
|
||||||
f"curl -s --show-error --max-time 3 http://{self.plan.bundle_ip}:9099 "
|
f"curl -s --show-error --max-time 3 http://{self.plan.bundle_ip}:9099 "
|
||||||
"2>&1 || true"
|
"2>&1 || true"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -256,57 +256,6 @@ class TestGitHttpBackend(unittest.TestCase):
|
|||||||
os.environ["GIT_GATE_ACCESS_HOOK"] = value
|
os.environ["GIT_GATE_ACCESS_HOOK"] = value
|
||||||
|
|
||||||
|
|
||||||
class TestMalformedStatusHeader(unittest.TestCase):
|
|
||||||
"""Malformed CGI Status: headers must not propagate as unhandled exceptions;
|
|
||||||
the handler should fall back to HTTP 500."""
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
from http.server import ThreadingHTTPServer
|
|
||||||
import tempfile
|
|
||||||
self._tmp = tempfile.mkdtemp()
|
|
||||||
os.environ["GIT_PROJECT_ROOT"] = self._tmp
|
|
||||||
self._server = ThreadingHTTPServer(("127.0.0.1", 0), GitHttpHandler)
|
|
||||||
self._thread = threading.Thread(
|
|
||||||
target=self._server.serve_forever, daemon=True,
|
|
||||||
)
|
|
||||||
self._thread.start()
|
|
||||||
self._port = self._server.server_port
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self._server.shutdown()
|
|
||||||
self._server.server_close()
|
|
||||||
os.environ.pop("GIT_PROJECT_ROOT", None)
|
|
||||||
import shutil
|
|
||||||
shutil.rmtree(self._tmp, ignore_errors=True)
|
|
||||||
|
|
||||||
def _get_with_backend_response(self, cgi_response: bytes) -> int:
|
|
||||||
with mock.patch(
|
|
||||||
"bot_bottle.git_http_backend.subprocess.run",
|
|
||||||
return_value=mock.Mock(returncode=0, stdout=cgi_response),
|
|
||||||
):
|
|
||||||
req = urllib.request.Request(
|
|
||||||
f"http://127.0.0.1:{self._port}/repo.git/info/refs",
|
|
||||||
method="GET",
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
with urllib.request.urlopen(req, timeout=3) as resp:
|
|
||||||
return resp.status
|
|
||||||
except urllib.error.HTTPError as e: # type: ignore
|
|
||||||
return e.code
|
|
||||||
|
|
||||||
def test_empty_status_value_returns_500(self):
|
|
||||||
status = self._get_with_backend_response(
|
|
||||||
b"Status: \r\nContent-Type: text/plain\r\n\r\n"
|
|
||||||
)
|
|
||||||
self.assertEqual(500, status)
|
|
||||||
|
|
||||||
def test_non_numeric_status_returns_500(self):
|
|
||||||
status = self._get_with_backend_response(
|
|
||||||
b"Status: bad\r\nContent-Type: text/plain\r\n\r\n"
|
|
||||||
)
|
|
||||||
self.assertEqual(500, status)
|
|
||||||
|
|
||||||
|
|
||||||
class TestContentLengthBounds(unittest.TestCase):
|
class TestContentLengthBounds(unittest.TestCase):
|
||||||
"""PRD 0041: malformed or oversized Content-Length is rejected before
|
"""PRD 0041: malformed or oversized Content-Length is rejected before
|
||||||
git http-backend is invoked."""
|
git http-backend is invoked."""
|
||||||
|
|||||||
@@ -50,15 +50,15 @@ from bot_bottle.supervise_server import (
|
|||||||
|
|
||||||
|
|
||||||
class TestValidation(unittest.TestCase):
|
class TestValidation(unittest.TestCase):
|
||||||
def test_capability_block_accepts_anything_nonempty(self):
|
|
||||||
validate_proposed_file(
|
|
||||||
_sv.TOOL_CAPABILITY_BLOCK,
|
|
||||||
"FROM python:3.13\nRUN apk add git\n",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_empty_proposed_file_rejected_for_tools_with_file_field(self):
|
def test_empty_proposed_file_rejected_for_tools_with_file_field(self):
|
||||||
with self.assertRaises(_RpcError):
|
with self.assertRaises(_RpcError):
|
||||||
validate_proposed_file(_sv.TOOL_CAPABILITY_BLOCK, " \n\t")
|
validate_proposed_file(_sv.TOOL_EGRESS_ALLOW, " \n\t")
|
||||||
|
|
||||||
|
def test_capability_block_rejected_as_unknown_tool(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
validate_proposed_file("capability-block", "FROM python:3.13\n")
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("unknown tool", cm.exception.message)
|
||||||
|
|
||||||
def test_egress_routes_yaml_is_validated(self):
|
def test_egress_routes_yaml_is_validated(self):
|
||||||
validate_proposed_file(
|
validate_proposed_file(
|
||||||
@@ -127,9 +127,9 @@ class TestRpcInternalErrorOnIoFailure(unittest.TestCase):
|
|||||||
with self.assertRaises(_RpcInternalError) as cm:
|
with self.assertRaises(_RpcInternalError) as cm:
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "x",
|
"justification": "x",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -219,7 +219,6 @@ class TestHandleToolsList(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted([
|
sorted([
|
||||||
_sv.TOOL_EGRESS_ALLOW,
|
_sv.TOOL_EGRESS_ALLOW,
|
||||||
_sv.TOOL_CAPABILITY_BLOCK,
|
|
||||||
_sv.TOOL_EGRESS_BLOCK,
|
_sv.TOOL_EGRESS_BLOCK,
|
||||||
_sv.TOOL_LIST_EGRESS_ROUTES,
|
_sv.TOOL_LIST_EGRESS_ROUTES,
|
||||||
]),
|
]),
|
||||||
@@ -295,10 +294,10 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_BLOCK,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "need git",
|
"justification": "need example.com",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
self.config,
|
self.config,
|
||||||
@@ -335,9 +334,9 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "needed for tests",
|
"justification": "needed for tests",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -359,20 +358,52 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
with self.assertRaises(_RpcError):
|
with self.assertRaises(_RpcError):
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {"dockerfile": "FROM python:3.13\n"},
|
"arguments": {"routes_yaml": "routes:\n - host: example.com\n"},
|
||||||
},
|
},
|
||||||
self.config,
|
self.config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_missing_name_raises(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call({"arguments": {}}, self.config)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
|
||||||
|
def test_arguments_must_be_object(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call(
|
||||||
|
{
|
||||||
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
|
"arguments": [],
|
||||||
|
},
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("must be an object", cm.exception.message)
|
||||||
|
|
||||||
|
def test_capability_block_call_raises_unknown_tool(self):
|
||||||
|
with self.assertRaises(_RpcError) as cm:
|
||||||
|
handle_tools_call(
|
||||||
|
{
|
||||||
|
"name": "capability-block",
|
||||||
|
"arguments": {
|
||||||
|
"dockerfile": "FROM python:3.13\n",
|
||||||
|
"justification": "need git",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
self.config,
|
||||||
|
)
|
||||||
|
self.assertEqual(ERR_INVALID_PARAMS, cm.exception.code)
|
||||||
|
self.assertIn("unknown tool", cm.exception.message)
|
||||||
|
|
||||||
def test_archives_proposal_after_response(self):
|
def test_archives_proposal_after_response(self):
|
||||||
responder = self._respond_when_proposal_appears(_sv.STATUS_APPROVED)
|
responder = self._respond_when_proposal_appears(_sv.STATUS_APPROVED)
|
||||||
try:
|
try:
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "x",
|
"justification": "x",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -394,10 +425,10 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
result = handle_tools_call(
|
result = handle_tools_call(
|
||||||
{
|
{
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "need a capability",
|
"justification": "need egress",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config,
|
config,
|
||||||
@@ -412,6 +443,31 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class TestHandleListEgressRoutes(unittest.TestCase):
|
class TestHandleListEgressRoutes(unittest.TestCase):
|
||||||
|
def test_success_returns_body_text(self):
|
||||||
|
class _Resp:
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: object) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return b"[{\"host\": \"example.com\"}]"
|
||||||
|
|
||||||
|
class _Opener:
|
||||||
|
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
||||||
|
return _Resp()
|
||||||
|
|
||||||
|
with patch.object(supervise_server.urllib.request, "build_opener", return_value=_Opener()):
|
||||||
|
result = handle_list_egress_routes(
|
||||||
|
{},
|
||||||
|
ServerConfig(bottle_slug="dev", queue_dir=Path("/unused")),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertFalse(result["isError"]) # type: ignore[index]
|
||||||
|
text = result["content"][0]["text"] # type: ignore[index]
|
||||||
|
self.assertIn("example.com", text)
|
||||||
|
|
||||||
def test_url_error_returns_tool_error(self):
|
def test_url_error_returns_tool_error(self):
|
||||||
class _Opener:
|
class _Opener:
|
||||||
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
def open(self, *args, **kwargs): # noqa: ANN001, ANN002, ANN003 # type: ignore
|
||||||
@@ -471,6 +527,13 @@ class TestFormatResponseText(unittest.TestCase):
|
|||||||
self.assertIn("the operator modified", text.lower())
|
self.assertIn("the operator modified", text.lower())
|
||||||
|
|
||||||
|
|
||||||
|
class TestFormatPendingResponseText(unittest.TestCase):
|
||||||
|
def test_formats_timeout_message(self):
|
||||||
|
text = supervise_server.format_pending_response_text(12.5)
|
||||||
|
self.assertIn("status: pending", text)
|
||||||
|
self.assertIn("12.5s", text)
|
||||||
|
|
||||||
|
|
||||||
# --- End-to-end HTTP sanity ------------------------------------------------
|
# --- End-to-end HTTP sanity ------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
@@ -521,7 +584,7 @@ class TestHttpEndToEnd(unittest.TestCase):
|
|||||||
self.assertEqual("2.0", result["jsonrpc"])
|
self.assertEqual("2.0", result["jsonrpc"])
|
||||||
self.assertEqual(1, result["id"])
|
self.assertEqual(1, result["id"])
|
||||||
names = [t["name"] for t in result["result"]["tools"]] # type: ignore[index]
|
names = [t["name"] for t in result["result"]["tools"]] # type: ignore[index]
|
||||||
self.assertIn(_sv.TOOL_CAPABILITY_BLOCK, names)
|
self.assertNotIn("capability-block", names)
|
||||||
self.assertIn(_sv.TOOL_EGRESS_ALLOW, names)
|
self.assertIn(_sv.TOOL_EGRESS_ALLOW, names)
|
||||||
self.assertIn(_sv.TOOL_EGRESS_BLOCK, names)
|
self.assertIn(_sv.TOOL_EGRESS_BLOCK, names)
|
||||||
|
|
||||||
@@ -541,9 +604,9 @@ class TestHttpEndToEnd(unittest.TestCase):
|
|||||||
"id": 99,
|
"id": 99,
|
||||||
"method": "tools/call",
|
"method": "tools/call",
|
||||||
"params": {
|
"params": {
|
||||||
"name": _sv.TOOL_CAPABILITY_BLOCK,
|
"name": _sv.TOOL_EGRESS_ALLOW,
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"dockerfile": "FROM python:3.13\n",
|
"routes_yaml": "routes:\n - host: example.com\n",
|
||||||
"justification": "x",
|
"justification": "x",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user