mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-09-30 22:03:14 +00:00
* feat: add plan mode for read-only research and planning
Adds a per-run plan_mode flag that puts the agent in a read-only
research phase: a strong prompt section is injected and mutating tools
are stripped via ExcludeToolsMiddleware so the agent proposes a
reviewable implementation plan before any edits. Surfaced in the
dashboard UI with a Plan toggle (Shift+Tab) wired through the thread API.
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
* fix: enforce plan-mode read-only at tool layer and disable subagents
Addresses PR review: plan mode previously relied on prompt text to keep
the shell read-only and left the task subagent (built with its own
write/PR/Linear tools) unrestricted. Now `task` is excluded so research
cannot be delegated to a mutating subagent, and a new
PlanModeShellGuardMiddleware enforces a read-only command allowlist on
`execute`, blocking writes, git state changes, installs, redirection,
and command substitution regardless of model/prompt-injection compliance.
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
* fix: harden plan-mode shell guard against wrapped mutations
Block git global options that take values (-C, --git-dir, ...) from being
misread as the subcommand, reject config-injection options (-c,
--config-env, --exec-path), and drop the env command wrapper that could
run arbitrary commands.
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
* feat: add plan mode with enter_plan_mode tool, profile/team defaults, Slack commands and approval flow
- enter_plan_mode tool: agent self-activates plan mode via Command(update={'plan_mode': True})
- Plan mode resolution: per-thread > profile default > team default > False
- PLAN_MODE_GUIDANCE_SECTION: always-present prompt section telling agent about the tool
- profile_plan_mode_default and team plan_mode_default settings
- Slack plan on/off/status commands with thread metadata persistence
- slack_thread_reply plan_approval=True renders Approve/Revise/Cancel buttons
- Interactivity handler: approve triggers implementation run, cancel posts confirmation
- Frontend: plan_mode_default in Profile/ProfileUpdate/TeamSettings types and UI toggles
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
* test: add tests for enter_plan_mode tool, profile/team defaults, Slack plan commands, approval blocks
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
* refactor(plan-mode): drop shell guard, rely on prompt for read-only discipline
Remove PlanModeShellGuardMiddleware and its enforcement of read-only shell
commands during plan mode. Plan mode now relies on the system prompt to
instruct the agent not to run mutating commands; the mutating-tool exclusion
(ExcludeToolsMiddleware) is retained.
* test(open-swe): add Playwright E2E for the Slack → PR → web handoff
Local, secrets-free end-to-end suite that drives the full happy path through mock Slack/GitHub control panels and the real dashboard UI. Only the LLM and external SaaS HTTP boundaries (GitHub/Slack APIs, OAuth token mint) are faked — the real process_slack_mention, get_agent, deepagents loop, tools, middleware, and dashboard authorization all run under `langgraph dev` with a scripted fake chat model and a local temp-dir sandbox.
- full_flow: a Slack mention runs the agent, which implements a change in the sandbox, opens a PR against a fake GitHub remote, and replies with the PR link in the same thread.
- dashboard: clicking the bot's real "Open in Web" link loads the built ui/ app (served same-origin); the thread owner can continue the conversation, while a different user sees the same thread read-only (no composer).
Wired into Agent CI as a `Playwright E2E` job that runs on pull requests.
* fix(open-swe): serve E2E UI assets via explicit route; pin Playwright
The dashboard E2E served the built ui/ SPA's /assets via app.mount(StaticFiles), but LangGraph's custom-app loader serves APIRoutes and drops sub-app Mounts, so /assets 404'd under `langgraph dev` in CI — the React app never booted and the composer/transcript never rendered. Serve assets via an explicit route instead.
Also pin @playwright/test to the latest (1.61.0) for reproducible runs, and make the owner composer assertion tolerant of either hydration state.
* test(open-swe): record Playwright trace + video on every E2E run
Capture a replayable trace (DOM snapshots, network, console, source) and a screen recording for every test, not just retries, plus a screenshot on failure. The CI job already uploads playwright-report/ and test-results/, so each run now has a downloadable replay; documented how to open it.
* feat(plan-mode): collaborative plan review with BlockNote + Yjs
When the agent enters plan mode it writes the plan as a markdown file in the
sandbox (save_plan tool), publishes it, and posts a review link to the source
channel. Reviewers open the plan inside the dashboard (under the /agents shell),
read it rendered in a BlockNote editor, and leave inline comments synced live
over Yjs. Only the thread owner can approve; any reviewer can request changes.
On approve/reject the comments are harvested and handed to the agent for the
follow-up run; the agent never sees comments mid-review.
- agent: enter_plan_mode persists plan state; new save_plan tool; prompt shares
the plan-review link.
- dashboard: Yjs WebSocket collab server (pycrdt-websocket) with store-backed
snapshots; plan content/status store; plan REST API (get/approve/reject,
owner-only approve, client-harvested comments); planStatus on thread summaries.
- ui: BlockNote native comments (CommentsExtension + YjsThreadStore) plan page
mounted under the agents shell, with a "Review plan" banner in the thread view
and a back-link; theme-aware (dark mode) using the dashboard tokens.
- e2e: Playwright coverage of the full Slack -> plan -> review -> approve -> PR
flow, including cross-user comment sync and owner-only approval.
* fix(plan-mode): address review feedback (authz, overrides, leaks, deps)
- plan-collab WS: authorize per-thread before joining a room (same read gate as
the REST API) — previously any logged-in user could join any thread (IDOR).
- plan-collab: tie the snapshot flusher to active connections (refcount) so each
opened plan no longer leaks a permanent 1.5s task on the shared event loop.
- plan decisions: include thread_id in the follow-up run configurable so the run
resumes the existing thread; set plan_mode explicitly so approve forces it off.
- get_agent: an explicit per-thread plan_mode (Slack `plan off`, approved plan,
dashboard toggle) now overrides profile/team defaults instead of falling back.
- plan mode tool gating moved to a state-aware PlanModeMiddleware installed
unconditionally, so a mid-run enter_plan_mode restricts the next model turn;
before_agent resets stale plan_mode so a later run isn't forced back into it.
- exclude write-capable http_request from plan mode.
- pin pycrdt / pycrdt-websocket with upper bounds.
Includes the latest base (#1583): E2E UI assets served via explicit route
(fixes the Playwright CI failure — LangGraph's app loader drops sub-app mounts).
* style: ruff format plan_collab.py
* fix(plan-mode): owner-gate Slack approval + same-origin check on collab WS
- Slack "Approve & Implement" now verifies the clicking user is the plan
requester (owner, via the stored triggering_user_id) before implementing —
matching the dashboard API's owner-only approval. Non-owners are pointed to
Revise / feedback.
- The plan-collab WebSocket validates the handshake Origin against the dashboard
allowlist before accept() (no-op when unconfigured, e.g. local/dev), mirroring
the REST require_same_origin CSRF defense.
* fix(plan-mode): enter plan mode only via the model + local mock dev harness
Plan mode is now entered solely when the model calls enter_plan_mode.
Removed the per-user and team plan_mode_default settings (backend + UI)
and the Slack `plan on/off/status` toggle.
- enter_plan_mode returns a terminating ToolMessage, fixing the missing
ToolMessage error that silently dropped plan mode mid-run.
- PlanReview: defer Yjs provider/doc teardown so React StrictMode's dev
remount doesn't destroy and then reuse the collaboration provider.
- e2e plan_review spec asserts plan_mode actually engages.
- LangSmith trace-url resolution is best-effort: bail before any API
call when the tenant is unset, cache failures, log at debug.
- Add `pnpm run dev:mock`: same-origin Vite HMR harness with a real LLM,
Alice/Bob mock users, and a GitHub login picker.
* docs(plan-mode): drop stale references to removed profile/team defaults
The plan_mode middleware docstring and the approve/reject dispatch comment
still described the profile/team plan_mode_default resolution that no longer
exists; reword to match model-driven entry + the per-thread carry.
* feat(plan-mode): let any reviewer edit the plan, not just comment
Drop the owner/commenter split for the plan document: everyone with read
access edits and comments alike (DefaultThreadStoreAuth "editor" for all,
editor always editable until a decision, anyone seeds the empty doc). This
matches the collab WS, which already relays frames to every readable user.
Plan approval stays owner-gated.
* test(plan-mode): assert plan-mode entry via the tool's success message
plan_mode lives only in run state for tool gating; it is not a persisted
thread-state channel, so the previous `values.plan_mode === true` poll
could never pass. Assert instead that enter_plan_mode's success ToolMessage
("Plan mode is active …") lands in the thread — which only happens when the
tool's Command applies cleanly, the exact regression this guards.
---------
Co-authored-by: Johannes du Plessis <51395795+johannes117@users.noreply.github.com>
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
536 lines
19 KiB
Python
536 lines
19 KiB
Python
"""HTTP app for the full-flow E2E (served as langgraph dev's http.app).
|
||
|
||
Mounts, on top of the REAL ``agent.webapp`` app:
|
||
- fake GitHub REST API (/fake-gh/...) the real open_pull_request hits this
|
||
- fake Slack API (/fake-slack/...) the real slack code hits this
|
||
- mock UIs (/mock/slack, /mock/github) what the user/Playwright sees
|
||
- control + compose (/control/*, /mock/slack/send) the test driver
|
||
|
||
Nothing here touches agent logic — it only stands in for the SaaS boundaries
|
||
and renders their state back as a user-facing UI.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import hmac
|
||
import json
|
||
import os
|
||
import sys
|
||
import time
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
import e2e_env # noqa: E402
|
||
import patches # noqa: E402
|
||
|
||
patches.apply()
|
||
|
||
import fakes # noqa: E402
|
||
import httpx # noqa: E402
|
||
from e2e_env import ( # noqa: E402
|
||
BASE_URL,
|
||
BOT_USER_ID,
|
||
DEMO_CHANNEL,
|
||
HUMAN_USER,
|
||
REPO_ROOT,
|
||
TEST_USERS,
|
||
)
|
||
from fastapi import HTTPException, Request # noqa: E402
|
||
from fastapi.responses import ( # noqa: E402
|
||
FileResponse,
|
||
HTMLResponse,
|
||
JSONResponse,
|
||
RedirectResponse,
|
||
Response,
|
||
)
|
||
|
||
# Slack-user directory the fake ``users.info`` resolves: the default sender used
|
||
# by the automated tests plus the named manual-test users.
|
||
_SLACK_USERS: dict[str, dict[str, str]] = {
|
||
HUMAN_USER: {"name": "devuser", "real_name": "Dev User", "email": "dev@example.com"},
|
||
**{
|
||
u["slack_id"]: {"name": u["login"], "real_name": u["name"], "email": u["email"]}
|
||
for u in TEST_USERS
|
||
},
|
||
}
|
||
|
||
from agent.dashboard.oauth import COOKIE_NAME, issue_session # noqa: E402
|
||
from agent.webapp import app, generate_thread_id_from_slack_thread # noqa: E402
|
||
|
||
GITHUB_WEBHOOK_SECRET = os.environ["GITHUB_WEBHOOK_SECRET"]
|
||
SLACK_SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
|
||
STATIC_DIR = Path(__file__).parent / "static"
|
||
|
||
CURRENT_THREAD: dict[str, str | None] = {"channel": DEMO_CHANNEL, "thread_ts": None}
|
||
|
||
fakes.seed_bare_remote()
|
||
|
||
|
||
# --- control + Slack compose (the test driver) -----------------------------
|
||
@app.post("/control/reset")
|
||
async def control_reset() -> JSONResponse:
|
||
fakes.reset()
|
||
CURRENT_THREAD["thread_ts"] = None
|
||
return JSONResponse({"ok": True})
|
||
|
||
|
||
@app.get("/control/state")
|
||
async def control_state() -> JSONResponse:
|
||
return JSONResponse(
|
||
{"channel": CURRENT_THREAD["channel"], "thread_ts": CURRENT_THREAD["thread_ts"]}
|
||
)
|
||
|
||
|
||
@app.post("/mock/slack/send")
|
||
async def slack_send(request: Request) -> JSONResponse:
|
||
"""Simulate a user posting in Slack: store the message, then deliver the
|
||
signed Events-API webhook to the real /webhooks/slack route."""
|
||
form = await request.json()
|
||
text = str(form.get("text", ""))
|
||
mention_bot = bool(form.get("mention_bot", True))
|
||
# Sender defaults to the first test user (Alice) — the canonical owner the
|
||
# automated tests log in as; the mock UI passes the chosen test user.
|
||
user_id = str(form.get("user") or TEST_USERS[0]["slack_id"])
|
||
channel = DEMO_CHANNEL
|
||
|
||
ts = fakes.new_thread_ts()
|
||
CURRENT_THREAD["thread_ts"] = ts
|
||
fakes.add_slack_message(channel, ts, user=user_id, text=text, is_bot=False)
|
||
|
||
payload = {
|
||
"type": "event_callback",
|
||
"event_id": f"Ev{ts}",
|
||
"authorizations": [{"user_id": BOT_USER_ID}],
|
||
"event": {
|
||
"type": "app_mention" if mention_bot else "message",
|
||
"channel": channel,
|
||
"user": user_id,
|
||
"text": text,
|
||
"ts": ts,
|
||
"thread_ts": ts,
|
||
},
|
||
}
|
||
raw = json.dumps(payload).encode()
|
||
req_ts = str(int(time.time()))
|
||
base = f"v0:{req_ts}:{raw.decode()}".encode()
|
||
sig = "v0=" + hmac.new(SLACK_SIGNING_SECRET.encode(), base, hashlib.sha256).hexdigest()
|
||
|
||
transport = httpx.ASGITransport(app=app)
|
||
async with httpx.AsyncClient(transport=transport, base_url="http://harness") as client:
|
||
resp = await client.post(
|
||
"/webhooks/slack",
|
||
content=raw,
|
||
headers={
|
||
"X-Slack-Signature": sig,
|
||
"X-Slack-Request-Timestamp": req_ts,
|
||
"Content-Type": "application/json",
|
||
},
|
||
)
|
||
return JSONResponse(
|
||
{
|
||
"thread_ts": ts,
|
||
"thread_id": generate_thread_id_from_slack_thread(channel, ts),
|
||
"webhook_status": resp.status_code,
|
||
"webhook": resp.json(),
|
||
}
|
||
)
|
||
|
||
|
||
@app.post("/control/login")
|
||
async def control_login(request: Request) -> JSONResponse:
|
||
"""Simulate a signed-in dashboard user by minting the real session cookie."""
|
||
form = await request.json()
|
||
login = str(form.get("login", "dev-user"))
|
||
email = str(form.get("email", "dev@example.com"))
|
||
token = issue_session(login=login, email=email, avatar_url=None)
|
||
resp = JSONResponse({"ok": True, "login": login, "email": email})
|
||
resp.set_cookie(COOKIE_NAME, token, httponly=True, samesite="lax", secure=False, path="/")
|
||
return resp
|
||
|
||
|
||
@app.get("/control/login")
|
||
async def control_login_get(login: str = "", email: str = "", next_url: str = "") -> Response:
|
||
"""Browser login. With no ``login``, render a dropdown of the test users;
|
||
with ``?login=<u>`` (email resolved from the registry, or pass ``&email=``),
|
||
mint the session cookie and redirect into the dashboard. Use a separate
|
||
browser/profile per user — each has its own cookie jar."""
|
||
# Land on the dashboard origin (DASHBOARD_BASE_URL — the Vite HMR server in
|
||
# dev:mock), not this harness, so the cookie + the hot-reloading UI line up.
|
||
ui = os.environ.get("DASHBOARD_BASE_URL", "").rstrip("/")
|
||
dest = next_url or (f"{ui}/agents" if ui else "/agents")
|
||
if not login:
|
||
options = "".join(f'<option value="{u["login"]}">{u["name"]}</option>' for u in TEST_USERS)
|
||
return HTMLResponse(
|
||
f"""<!doctype html><meta charset=utf-8><title>Mock login</title>
|
||
<body style="font-family:system-ui;max-width:420px;margin:3rem auto;padding:0 1rem">
|
||
<h1 style="font-size:1.1rem">Sign in (mock)</h1>
|
||
<form method=get action=/control/login>
|
||
<select name=login style="font:inherit;padding:0.4rem">{options}</select>
|
||
<button style="font:inherit;padding:0.45rem 0.9rem;cursor:pointer">Sign in</button>
|
||
</form>
|
||
<p style="color:#888;font-size:0.85rem">Tip: use a separate browser or profile per
|
||
user so their sessions don't overwrite each other.</p>
|
||
</body>"""
|
||
)
|
||
if not email:
|
||
match = next((u for u in TEST_USERS if u["login"] == login), None)
|
||
email = match["email"] if match else f"{login}@example.com"
|
||
token = issue_session(login=login, email=email, avatar_url=None)
|
||
resp = RedirectResponse(url=dest, status_code=303)
|
||
resp.set_cookie(COOKIE_NAME, token, httponly=True, samesite="lax", secure=False, path="/")
|
||
return resp
|
||
|
||
|
||
@app.get("/dashboard/api/auth/login")
|
||
async def mock_github_login(redirect_to: str = "", login: str = "") -> Response:
|
||
"""Mock stand-in for GitHub OAuth: the dashboard's "Continue with GitHub"
|
||
button lands here. With no ``login``, render a picker of the fake GitHub
|
||
test users; once one is chosen, mint the real session cookie and redirect
|
||
back into the dashboard (``redirect_to``)."""
|
||
ui = os.environ.get("DASHBOARD_BASE_URL", "").rstrip("/")
|
||
dest = redirect_to or (f"{ui}/agents" if ui else "/agents")
|
||
if not login:
|
||
options = "".join(
|
||
f'<option value="{u["login"]}">{u["name"]} (@{u["login"]})</option>' for u in TEST_USERS
|
||
)
|
||
return HTMLResponse(
|
||
f"""<!doctype html><meta charset=utf-8><title>Continue with GitHub (mock)</title>
|
||
<body style="font-family:system-ui;max-width:420px;margin:3rem auto;padding:0 1rem">
|
||
<h1 style="font-size:1.1rem">Continue with GitHub (mock)</h1>
|
||
<p style="color:#888;font-size:0.9rem">Pick a fake GitHub account to sign in as.</p>
|
||
<form method=get action=/dashboard/api/auth/login>
|
||
<input type=hidden name=redirect_to value="{dest}">
|
||
<select name=login style="font:inherit;padding:0.4rem">{options}</select>
|
||
<button style="font:inherit;padding:0.45rem 0.9rem;cursor:pointer">Continue</button>
|
||
</form>
|
||
<p style="color:#888;font-size:0.85rem">Tip: use a separate browser or profile per
|
||
user so their sessions don't overwrite each other.</p>
|
||
</body>"""
|
||
)
|
||
match = next((u for u in TEST_USERS if u["login"] == login), None)
|
||
email = match["email"] if match else f"{login}@example.com"
|
||
token = issue_session(login=login, email=email, avatar_url=None)
|
||
resp = RedirectResponse(url=dest, status_code=303)
|
||
resp.set_cookie(COOKIE_NAME, token, httponly=True, samesite="lax", secure=False, path="/")
|
||
return resp
|
||
|
||
|
||
# The real dashboard registered /dashboard/api/auth/login first (via
|
||
# include_router), so Starlette would match it before ours. Move ours to the
|
||
# front of the table so the mock picker shadows the real OAuth redirect.
|
||
for _i, _route in enumerate(app.router.routes):
|
||
if getattr(_route, "endpoint", None) is mock_github_login:
|
||
app.router.routes.insert(0, app.router.routes.pop(_i))
|
||
break
|
||
|
||
|
||
@app.post("/control/logout")
|
||
async def control_logout() -> JSONResponse:
|
||
resp = JSONResponse({"ok": True})
|
||
resp.delete_cookie(COOKIE_NAME, path="/")
|
||
return resp
|
||
|
||
|
||
# --- serve the REAL built ui/ SPA, same-origin so the session cookie works ----
|
||
# The "Open in Web" link (DASHBOARD_BASE_URL/agents/{id}) lands on the real app;
|
||
# it calls /dashboard/api/* (same origin) and streams via the dashboard proxy.
|
||
UI_PUBLIC = REPO_ROOT / "ui" / ".output" / "public"
|
||
_ASSETS_ROOT = (UI_PUBLIC / "assets").resolve()
|
||
|
||
|
||
def _ui_file(name: str) -> FileResponse:
|
||
path = UI_PUBLIC / name
|
||
if not path.is_file():
|
||
raise HTTPException(404, f"{name} not built — run `bun run build` in ui/")
|
||
return FileResponse(path)
|
||
|
||
|
||
@app.get("/assets/{asset_path:path}")
|
||
async def ui_asset(asset_path: str) -> FileResponse:
|
||
# Explicit route, not app.mount(StaticFiles): LangGraph's custom-app loader
|
||
# serves APIRoutes but drops sub-app Mounts, so a mount 404s under it.
|
||
target = (_ASSETS_ROOT / asset_path).resolve()
|
||
if not str(target).startswith(str(_ASSETS_ROOT)) or not target.is_file():
|
||
raise HTTPException(404, "asset not found")
|
||
return FileResponse(target)
|
||
|
||
|
||
@app.get("/_shell.html", response_class=HTMLResponse)
|
||
async def ui_shell() -> FileResponse:
|
||
return _ui_file("_shell.html")
|
||
|
||
|
||
@app.get("/manifest.webmanifest")
|
||
async def ui_manifest() -> FileResponse:
|
||
return _ui_file("manifest.webmanifest")
|
||
|
||
|
||
@app.get("/favicon.png")
|
||
async def ui_favicon() -> FileResponse:
|
||
return _ui_file("favicon.png")
|
||
|
||
|
||
@app.get("/apple-touch-icon.png")
|
||
async def ui_apple_icon() -> FileResponse:
|
||
return _ui_file("apple-touch-icon.png")
|
||
|
||
|
||
@app.get("/logo-mark.png")
|
||
async def ui_logo_mark() -> FileResponse:
|
||
return _ui_file("logo-mark.png")
|
||
|
||
|
||
# Client routes used by the handoff tests: serve the SPA shell; the client
|
||
# router boots at the current URL. Kept explicit (no catch-all) so LangGraph's
|
||
# own root routes — which the dashboard proxy calls server-side — are untouched.
|
||
@app.get("/agents/{thread_id}", response_class=HTMLResponse)
|
||
async def ui_agents_thread(thread_id: str) -> FileResponse: # noqa: ARG001
|
||
return _ui_file("_shell.html")
|
||
|
||
|
||
@app.get("/agents/{thread_id}/plan", response_class=HTMLResponse)
|
||
async def ui_agents_plan(thread_id: str) -> FileResponse: # noqa: ARG001
|
||
return _ui_file("_shell.html")
|
||
|
||
|
||
@app.get("/login", response_class=HTMLResponse)
|
||
async def ui_login() -> FileResponse:
|
||
return _ui_file("_shell.html")
|
||
|
||
|
||
@app.get("/mock/users")
|
||
async def mock_users() -> JSONResponse:
|
||
"""The named test users that drive the Slack sender + login dropdowns."""
|
||
return JSONResponse(TEST_USERS)
|
||
|
||
|
||
@app.get("/mock/slack/messages")
|
||
async def slack_messages() -> JSONResponse:
|
||
thread = CURRENT_THREAD["thread_ts"]
|
||
msgs = fakes.slack_thread(CURRENT_THREAD["channel"], thread) if thread else []
|
||
return JSONResponse(
|
||
[{"user": m["user"], "text": m["text"], "is_bot": m["is_bot"], "ts": m["ts"]} for m in msgs]
|
||
)
|
||
|
||
|
||
# --- mock UIs --------------------------------------------------------------
|
||
@app.get("/mock/slack", response_class=HTMLResponse)
|
||
async def mock_slack_page() -> str:
|
||
return (STATIC_DIR / "slack.html").read_text()
|
||
|
||
|
||
@app.get("/mock/github", response_class=HTMLResponse)
|
||
async def mock_github_page() -> str:
|
||
return (STATIC_DIR / "github.html").read_text()
|
||
|
||
|
||
def _pr_html_url(pr: dict[str, Any]) -> str:
|
||
return f"{BASE_URL}/mock/github/{pr['owner']}/{pr['repo']}/pull/{pr['number']}"
|
||
|
||
|
||
@app.get("/mock/github/data")
|
||
async def mock_github_data() -> JSONResponse:
|
||
return JSONResponse(
|
||
[
|
||
{
|
||
"number": p["number"],
|
||
"title": p["title"],
|
||
"head": p["head"],
|
||
"base": p["base"],
|
||
"state": p["state"],
|
||
"draft": p["draft"],
|
||
"author": p["author"],
|
||
"body": p["body"],
|
||
"files": p["files"],
|
||
"url": _pr_html_url(p),
|
||
}
|
||
for p in fakes.PULLS
|
||
]
|
||
)
|
||
|
||
|
||
@app.get("/mock/github/{owner}/{repo}/pull/{number}", response_class=HTMLResponse)
|
||
async def mock_github_pr(owner: str, repo: str, number: int) -> HTMLResponse: # noqa: ARG001
|
||
pr = fakes.find_pull(number)
|
||
if pr is None:
|
||
return HTMLResponse(f"<h1>PR #{number} not found</h1>", status_code=404)
|
||
files = "".join(
|
||
f'<li data-file="{f["filename"]}">{f["filename"]} '
|
||
f"<span class='stat'>+{f['additions']} −{f['deletions']}</span></li>"
|
||
for f in pr["files"]
|
||
)
|
||
draft = " (draft)" if pr["draft"] else ""
|
||
return HTMLResponse(
|
||
f"""<!doctype html><meta charset=utf-8>
|
||
<title>PR #{pr["number"]} — {pr["owner"]}/{pr["repo"]}</title>
|
||
<body style="font-family:system-ui;max-width:720px;margin:2rem auto">
|
||
<p><a href="/mock/github">← all pull requests</a></p>
|
||
<h1 id="pr-title">{pr["title"]}{draft}</h1>
|
||
<p>#{pr["number"]} · <span id="pr-state">{pr["state"]}</span> ·
|
||
<code id="pr-head">{pr["head"]}</code> → <code>{pr["base"]}</code> ·
|
||
by <span id="pr-author">{pr["author"]}</span></p>
|
||
<h3>Description</h3><pre id="pr-body">{pr["body"]}</pre>
|
||
<h3>Files changed ({len(pr["files"])})</h3>
|
||
<ul id="pr-files">{files}</ul>
|
||
</body>"""
|
||
)
|
||
|
||
|
||
# --- fake GitHub REST API (open_pull_request hits this) --------------------
|
||
def _gh_pr_json(pr: dict[str, Any]) -> dict[str, Any]:
|
||
return {
|
||
"number": pr["number"],
|
||
"html_url": _pr_html_url(pr),
|
||
"state": pr["state"],
|
||
"draft": pr["draft"],
|
||
"merged": pr["merged"],
|
||
"title": pr["title"],
|
||
"body": pr["body"],
|
||
"user": {"login": pr["author"]},
|
||
"head": {"ref": pr["head"]},
|
||
"base": {"ref": pr["base"]},
|
||
"additions": pr["additions"],
|
||
"deletions": pr["deletions"],
|
||
"changed_files": len(pr["files"]),
|
||
}
|
||
|
||
|
||
@app.get("/fake-gh/repos/{owner}/{repo}")
|
||
async def gh_get_repo(owner: str, repo: str) -> JSONResponse:
|
||
return JSONResponse({"full_name": f"{owner}/{repo}", "private": False})
|
||
|
||
|
||
@app.get("/fake-gh/repos/{owner}/{repo}/pulls")
|
||
async def gh_list_pulls(owner: str, repo: str) -> JSONResponse: # noqa: ARG001
|
||
return JSONResponse([])
|
||
|
||
|
||
@app.post("/fake-gh/repos/{owner}/{repo}/pulls")
|
||
async def gh_create_pull(owner: str, repo: str, request: Request) -> JSONResponse:
|
||
body = await request.json()
|
||
pr = fakes.create_pull(
|
||
owner,
|
||
repo,
|
||
head=body.get("head", ""),
|
||
base=body.get("base", "main"),
|
||
title=body.get("title", ""),
|
||
body=body.get("body", ""),
|
||
draft=bool(body.get("draft", True)),
|
||
)
|
||
return JSONResponse(_gh_pr_json(pr), status_code=201)
|
||
|
||
|
||
@app.get("/fake-gh/repos/{owner}/{repo}/pulls/{number}")
|
||
async def gh_get_pull(owner: str, repo: str, number: int) -> JSONResponse: # noqa: ARG001
|
||
pr = fakes.find_pull(number)
|
||
if pr is None:
|
||
return JSONResponse({"message": "Not Found"}, status_code=404)
|
||
return JSONResponse(_gh_pr_json(pr))
|
||
|
||
|
||
# --- fake Slack API (real slack code hits this) ----------------------------
|
||
def _ok(extra: dict[str, Any] | None = None) -> JSONResponse:
|
||
return JSONResponse({"ok": True, **(extra or {})})
|
||
|
||
|
||
@app.post("/fake-slack/chat.postMessage")
|
||
async def slack_post_message(request: Request) -> JSONResponse:
|
||
body = await request.json()
|
||
ts = fakes.add_slack_message(
|
||
body.get("channel", ""),
|
||
body.get("thread_ts", ""),
|
||
user=BOT_USER_ID,
|
||
text=body.get("text", ""),
|
||
blocks=body.get("blocks"),
|
||
is_bot=True,
|
||
)
|
||
return _ok({"ts": ts, "message": {"ts": ts}})
|
||
|
||
|
||
@app.post("/fake-slack/chat.postEphemeral")
|
||
async def slack_post_ephemeral(request: Request) -> JSONResponse:
|
||
await request.body()
|
||
return _ok({"message_ts": fakes.next_slack_ts()})
|
||
|
||
|
||
@app.post("/fake-slack/assistant.threads.setStatus")
|
||
async def slack_set_status(request: Request) -> JSONResponse:
|
||
await request.body()
|
||
return _ok()
|
||
|
||
|
||
@app.post("/fake-slack/reactions.add")
|
||
async def slack_reactions_add(request: Request) -> JSONResponse:
|
||
await request.body()
|
||
return _ok()
|
||
|
||
|
||
@app.get("/fake-slack/users.info")
|
||
async def slack_users_info(user: str = "") -> JSONResponse:
|
||
info = _SLACK_USERS.get(
|
||
user, {"name": "devuser", "real_name": "Dev User", "email": "dev@example.com"}
|
||
)
|
||
return _ok(
|
||
{
|
||
"user": {
|
||
"id": user,
|
||
"name": info["name"],
|
||
"real_name": info["real_name"],
|
||
"profile": {
|
||
"email": info["email"],
|
||
"display_name": info["real_name"],
|
||
"real_name": info["real_name"],
|
||
},
|
||
}
|
||
}
|
||
)
|
||
|
||
|
||
@app.get("/fake-slack/conversations.info")
|
||
async def slack_conversations_info(channel: str = "") -> JSONResponse:
|
||
return _ok(
|
||
{
|
||
"channel": {
|
||
"id": channel,
|
||
"name": "demo",
|
||
"topic": {"value": ""},
|
||
"purpose": {"value": ""},
|
||
}
|
||
}
|
||
)
|
||
|
||
|
||
@app.get("/fake-slack/conversations.replies")
|
||
async def slack_conversations_replies(channel: str = "", ts: str = "") -> JSONResponse:
|
||
msgs = fakes.slack_thread(channel, ts)
|
||
return _ok(
|
||
{
|
||
"messages": [
|
||
{
|
||
"type": "message",
|
||
"user": m["user"],
|
||
"text": m["text"],
|
||
"ts": m["ts"],
|
||
"thread_ts": m["thread_ts"],
|
||
}
|
||
for m in msgs
|
||
]
|
||
}
|
||
)
|
||
|
||
|
||
@app.get("/fake-slack/conversations.history")
|
||
async def slack_conversations_history(channel: str = "") -> JSONResponse: # noqa: ARG001
|
||
return _ok({"messages": []})
|
||
|
||
|
||
@app.get("/fake-slack/chat.getPermalink")
|
||
async def slack_get_permalink(channel: str = "", message_ts: str = "") -> JSONResponse: # noqa: ARG001
|
||
return _ok({"permalink": f"{BASE_URL}/mock/slack"})
|
||
|
||
|
||
# Quietly reference imports used only for env side effects.
|
||
_ = (e2e_env, HUMAN_USER)
|