Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| afa8cd5dd9 | |||
| ced050b181 | |||
| 46f5de6619 | |||
| 500122711f | |||
| b00a83ae0d | |||
| 52bac82013 | |||
| 2323a9ae83 | |||
| c60993181a | |||
| 50f5b3aa7f | |||
| 45a096413f | |||
| c6479d62e4 | |||
| d0cad3a559 | |||
| c2ddac1be5 | |||
| 446414144e | |||
| 8188d6304e |
@@ -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
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ on:
|
|||||||
- '**.py'
|
- '**.py'
|
||||||
- '.pylintrc'
|
- '.pylintrc'
|
||||||
- 'pyrightconfig.json'
|
- 'pyrightconfig.json'
|
||||||
|
- '.coveragerc'
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -45,10 +46,19 @@ jobs:
|
|||||||
echo "errors=$ERRORS" >> $GITHUB_OUTPUT
|
echo "errors=$ERRORS" >> $GITHUB_OUTPUT
|
||||||
echo "Pyright errors: $ERRORS"
|
echo "Pyright errors: $ERRORS"
|
||||||
|
|
||||||
|
- name: Run coverage and extract percentage
|
||||||
|
id: coverage
|
||||||
|
run: |
|
||||||
|
python -m coverage run -m unittest discover -t . -s tests/unit > /dev/null 2>&1 || true
|
||||||
|
PERCENT=$(python -m coverage report 2>/dev/null | grep '^TOTAL' | grep -oP '\d+(?=%)' | tail -1)
|
||||||
|
echo "percent=$PERCENT" >> $GITHUB_OUTPUT
|
||||||
|
echo "Coverage: $PERCENT%"
|
||||||
|
|
||||||
- name: Update badges in README
|
- name: Update badges in README
|
||||||
run: |
|
run: |
|
||||||
PYLINT_SCORE="${{ steps.pylint.outputs.score }}"
|
PYLINT_SCORE="${{ steps.pylint.outputs.score }}"
|
||||||
PYRIGHT_ERRORS="${{ steps.pyright.outputs.errors }}"
|
PYRIGHT_ERRORS="${{ steps.pyright.outputs.errors }}"
|
||||||
|
COVERAGE_PERCENT="${{ steps.coverage.outputs.percent }}"
|
||||||
|
|
||||||
PYLINT_SCORE_ENCODED=$(echo "$PYLINT_SCORE" | sed 's|/|%2F|g')
|
PYLINT_SCORE_ENCODED=$(echo "$PYLINT_SCORE" | sed 's|/|%2F|g')
|
||||||
|
|
||||||
@@ -58,9 +68,12 @@ jobs:
|
|||||||
if [ -n "$PYRIGHT_ERRORS" ]; then
|
if [ -n "$PYRIGHT_ERRORS" ]; then
|
||||||
sed -i "s|/badge/pyright-[^)]*|/badge/pyright-${PYRIGHT_ERRORS}%20errors-brightgreen|" README.md
|
sed -i "s|/badge/pyright-[^)]*|/badge/pyright-${PYRIGHT_ERRORS}%20errors-brightgreen|" README.md
|
||||||
fi
|
fi
|
||||||
|
if [ -n "$COVERAGE_PERCENT" ]; then
|
||||||
|
sed -i "s|/badge/coverage-[^)]*|/badge/coverage-${COVERAGE_PERCENT}%25-brightgreen|" README.md
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Updated badges:"
|
echo "Updated badges:"
|
||||||
grep -E "pylint|pyright" README.md | head -2
|
grep -E "pylint|pyright|coverage" README.md | head -3
|
||||||
|
|
||||||
- name: Commit and push badge updates
|
- name: Commit and push badge updates
|
||||||
run: |
|
run: |
|
||||||
@@ -73,7 +86,7 @@ jobs:
|
|||||||
else
|
else
|
||||||
echo "Badge changes detected, committing..."
|
echo "Badge changes detected, committing..."
|
||||||
git add README.md
|
git add README.md
|
||||||
MSG="chore: update quality badges"$'\n\n'"- Pylint: ${{ steps.pylint.outputs.score }}"$'\n'"- Pyright: ${{ steps.pyright.outputs.errors }} errors"$'\n\n'"[skip ci]"
|
MSG="chore: update quality badges"$'\n\n'"- Pylint: ${{ steps.pylint.outputs.score }}"$'\n'"- Pyright: ${{ steps.pyright.outputs.errors }} errors"$'\n'"- Coverage: ${{ steps.coverage.outputs.percent }}%"$'\n\n'"[skip ci]"
|
||||||
git commit -m "$MSG"
|
git commit -m "$MSG"
|
||||||
git push
|
git push
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -22,3 +22,4 @@ venv/
|
|||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
.coverage
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
[](https://gitea.dideric.is/didericis/bot-bottle/actions?workflow=test.yml)
|
[](https://gitea.dideric.is/didericis/bot-bottle/actions?workflow=test.yml)
|
||||||
[](https://github.com/PyCQA/pylint)
|
[](https://github.com/PyCQA/pylint)
|
||||||
[](https://github.com/microsoft/pyright)
|
[](https://github.com/microsoft/pyright)
|
||||||
|
[](https://coverage.readthedocs.io/)
|
||||||
|
|
||||||
**Problem:** Developer wants to run a coding agent without supervision, but they don't want a prompt injected or misbehaving agent wrecking their environment or exfiltrating sensitive data.
|
**Problem:** Developer wants to run a coding agent without supervision, but they don't want a prompt injected or misbehaving agent wrecking their environment or exfiltrating sensitive data.
|
||||||
|
|
||||||
|
|||||||
@@ -319,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()
|
||||||
@@ -330,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()
|
||||||
@@ -420,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)"
|
||||||
@@ -471,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
|
||||||
@@ -523,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()
|
||||||
@@ -534,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()
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from bot_bottle.git_gate import (
|
from bot_bottle.git_gate import (
|
||||||
GitGate,
|
GitGate,
|
||||||
@@ -13,6 +14,8 @@ from bot_bottle.git_gate import (
|
|||||||
git_gate_render_access_hook,
|
git_gate_render_access_hook,
|
||||||
git_gate_render_entrypoint,
|
git_gate_render_entrypoint,
|
||||||
git_gate_render_hook,
|
git_gate_render_hook,
|
||||||
|
revoke_git_gate_provisioned_keys,
|
||||||
|
_resolve_identity_file,
|
||||||
git_gate_upstreams_for_bottle,
|
git_gate_upstreams_for_bottle,
|
||||||
)
|
)
|
||||||
from bot_bottle.manifest import ManifestIndex
|
from bot_bottle.manifest import ManifestIndex
|
||||||
@@ -328,6 +331,68 @@ class TestPrepare(unittest.TestCase):
|
|||||||
self.assertIn("exec git daemon", content)
|
self.assertIn("exec git daemon", content)
|
||||||
|
|
||||||
|
|
||||||
|
class TestDynamicKeyProvisioning(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.stage = Path(tempfile.mkdtemp())
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
shutil.rmtree(self.stage, ignore_errors=True)
|
||||||
|
|
||||||
|
def _gitea_manifest(self):
|
||||||
|
return ManifestIndex.from_json_obj({
|
||||||
|
"bottles": {
|
||||||
|
"dev": {
|
||||||
|
"git-gate": {
|
||||||
|
"repos": {
|
||||||
|
"repo": {
|
||||||
|
"url": "ssh://git@gitea.example.com/org/repo.git",
|
||||||
|
"key": {
|
||||||
|
"provider": "gitea",
|
||||||
|
"forge_token_env": "GITEA_TOKEN",
|
||||||
|
},
|
||||||
|
"host_key": "ssh-ed25519 AAAA...",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"agents": {"demo": {"skills": [], "prompt": "", "bottle": "dev"}},
|
||||||
|
})
|
||||||
|
|
||||||
|
def test_resolve_identity_file_static_uses_entry_path(self):
|
||||||
|
entry = fixture_with_git().bottles["dev"].git[0]
|
||||||
|
self.assertEqual(entry.IdentityFile, _resolve_identity_file(entry, "demo", self.stage))
|
||||||
|
|
||||||
|
def test_resolve_identity_file_gitea_provisions_key(self):
|
||||||
|
entry = self._gitea_manifest().bottles["dev"].git[0]
|
||||||
|
with patch("bot_bottle.git_gate._provision_dynamic_key", return_value="/tmp/provisioned-key") as mock_provision:
|
||||||
|
self.assertEqual("/tmp/provisioned-key", _resolve_identity_file(entry, "demo", self.stage))
|
||||||
|
mock_provision.assert_called_once()
|
||||||
|
|
||||||
|
def test_revoke_skips_non_gitea_and_missing_id_file(self):
|
||||||
|
revoke_git_gate_provisioned_keys(fixture_with_git().bottles["dev"], self.stage)
|
||||||
|
|
||||||
|
def test_revoke_calls_delete_for_gitea_entry(self):
|
||||||
|
bottle = self._gitea_manifest().bottles["dev"]
|
||||||
|
(self.stage / "repo-deploy-key-id").write_text("123\n")
|
||||||
|
with patch.dict("os.environ", {"GITEA_TOKEN": "token"}), patch(
|
||||||
|
"bot_bottle.deploy_key_provisioner.get_provisioner"
|
||||||
|
) as mock_get_provisioner:
|
||||||
|
provisioner = mock_get_provisioner.return_value
|
||||||
|
revoke_git_gate_provisioned_keys(bottle, self.stage)
|
||||||
|
mock_get_provisioner.assert_called_once()
|
||||||
|
provisioner.delete.assert_called_once_with("org/repo", "123")
|
||||||
|
|
||||||
|
def test_revoke_missing_token_raises(self):
|
||||||
|
bottle = self._gitea_manifest().bottles["dev"]
|
||||||
|
(self.stage / "repo-deploy-key-id").write_text("123\n")
|
||||||
|
with patch.dict("os.environ", {}, clear=True), self.assertRaises(RuntimeError) as cm:
|
||||||
|
revoke_git_gate_provisioned_keys(bottle, self.stage)
|
||||||
|
self.assertIn("env var is not set", str(cm.exception))
|
||||||
|
|
||||||
|
|
||||||
class TestShellEscaping(unittest.TestCase):
|
class TestShellEscaping(unittest.TestCase):
|
||||||
"""Regression tests: all three render functions must produce syntactically
|
"""Regression tests: all three render functions must produce syntactically
|
||||||
valid sh code even when names and upstream URLs contain shell-special
|
valid sh code even when names and upstream URLs contain shell-special
|
||||||
|
|||||||
@@ -364,6 +364,23 @@ class TestHandleToolsCall(unittest.TestCase):
|
|||||||
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):
|
def test_capability_block_call_raises_unknown_tool(self):
|
||||||
with self.assertRaises(_RpcError) as cm:
|
with self.assertRaises(_RpcError) as cm:
|
||||||
handle_tools_call(
|
handle_tools_call(
|
||||||
@@ -426,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
|
||||||
@@ -485,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 ------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user