Add a `browser` bridge: real browser automation behind the standard
token-guarded localhost JSON surface, so a codegen-authored skill body never
bundles a browser stack. It opens a URL, reads a page's text or links, takes a
screenshot, and clicks/fills/presses/selects to complete a flow.
Unlike the stdlib-only Nextcloud/Outlook bridges this one owns a browser and
needs a heavy optional dependency, so it is disabled by default and installed
with `scripts/bootstrap.sh --browser` (Playwright pinned in
requirements/browser.txt; the browser binary lands under the checkout's
.runtime/browser, which the driver points PLAYWRIGHT_BROWSERS_PATH at).
- `BrowserBridge` (`bridges/browser.py`) + `BrowserDriver`
(`bridges/browser_client.py`). A real navigation/timeout/selector failure is a
502, a missing package/browser a 503 install hint, bad input a 400 -- never a
fabricated success.
- `check_requirements()` deliberately never blocks: `run_bridges()` aborts all
bridges when one fails, and this dependency is optional, so a missing
Playwright must degrade to a per-call 503, not take down the others.
- Playwright's sync API is bound to its creating thread while the bridge serves
each request on a new one, so the driver runs the async API on one dedicated
event-loop thread and serializes whole operations behind an asyncio.Lock
(submitted with run_coroutine_threadsafe).
- Persistent profile under .runtime/browser/profile so logins/cookies survive
across calls and restarts; POST /reset with purge=true wipes it, and
headless=false shows the window for a first sign-in. Route set: /health,
/state, /content, /text, /links, /screenshot, /goto, /click, /fill, /press,
/select, /wait, /back, /forward, /reset. No arbitrary JS evaluate (out of
scope for this cut).
- Seeds: `seeds/browser/{open_page,read_page,screenshot}`, each hermetically
tested against a loopback bridge. A `browser` category description is seeded
so the leaf softmax has signal.
- `tests/test_browser_bridge.py` exercises the routes over a loopback HTTP
server with an injected fake driver (no browser or Playwright needed). The
live browser is a provisioned-host concern: this box could not reach the
Playwright CDN, so no real-browser run backs it yet.
89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
"""The committed seed skills are real, inspectable, and hermetically tested.
|
|
|
|
A seed is a starter integration shipped in the repo in the CODEGEN.md folder
|
|
format. These tests keep every seed honest: the declared integration must match
|
|
the code, the contract must cover what the body reads, and the hermetic
|
|
mechanics test must pass. They are not proof of the live integration — only a
|
|
real run against the user's service is (see AGENTS.md).
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from semif_agent.codegen import (
|
|
integration_findings,
|
|
parse_contract,
|
|
parse_integration,
|
|
run_skill_test,
|
|
)
|
|
|
|
SEEDS = Path(__file__).resolve().parent.parent / "seeds"
|
|
SEED_DIRS = sorted(path.parent for path in SEEDS.glob("*/*/skill.py"))
|
|
|
|
|
|
def seed_id(seed: Path) -> str:
|
|
return f"{seed.parent.name}.{seed.name}"
|
|
|
|
|
|
def test_expected_seeds_exist():
|
|
ids = {seed_id(seed) for seed in SEED_DIRS}
|
|
assert "nextcloud.next_event" in ids
|
|
assert "nextcloud.create_event" in ids
|
|
assert "simplex.next_message" in ids
|
|
assert "simplex.send_message" in ids
|
|
assert "simplex.connect_link" in ids
|
|
assert "time.now" in ids
|
|
assert "time.date" in ids
|
|
assert "time.set_timer" in ids
|
|
assert "time.set_alarm" in ids
|
|
assert "nextcloud.create_task" in ids
|
|
assert "calculator.calculate" in ids
|
|
assert "browser.open_page" in ids
|
|
assert "browser.read_page" in ids
|
|
assert "browser.screenshot" in ids
|
|
|
|
|
|
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
|
|
def test_seed_declares_a_real_integration(seed):
|
|
code = (seed / "skill.py").read_text()
|
|
integration = parse_integration(code)
|
|
assert integration["service"]
|
|
assert integration["transport"]
|
|
assert integration_findings(integration, "declared", code) == []
|
|
|
|
|
|
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
|
|
def test_seed_contract_is_flat_and_covered_by_declaration(seed):
|
|
code = (seed / "skill.py").read_text()
|
|
contract = parse_contract(code)
|
|
integration = parse_integration(code)
|
|
assert set(integration["config_vars"]) == set(contract)
|
|
mirror = json.loads((seed / "contract.json").read_text())
|
|
assert mirror == contract, "contract.json must mirror the CONTRACT constant"
|
|
if integration["transport"] == "compute":
|
|
# A local compute skill acts on the machine itself and needs no
|
|
# configuration; an empty contract is legitimate.
|
|
return
|
|
assert contract, "an external-integration seed must declare a CONTRACT constant"
|
|
for name, description in contract.items():
|
|
assert name.islower() and " " not in name, name
|
|
assert isinstance(description, str) and description.strip()
|
|
|
|
|
|
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
|
|
def test_seed_has_manifest_description(seed):
|
|
manifest = json.loads((seed / "manifest.json").read_text())
|
|
assert manifest["description"].strip()
|
|
|
|
|
|
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
|
|
def test_seed_hermetic_test_passes(seed, monkeypatch):
|
|
root = str(seed.parents[2])
|
|
existing = os.environ.get("PYTHONPATH", "")
|
|
monkeypatch.setenv("PYTHONPATH", root + os.pathsep + existing)
|
|
passed, output = run_skill_test(seed, timeout=30)
|
|
assert passed, output
|