Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
"""REST routes for the Agentic IDE's Git panel.
Endpoints (all under ``/api/agentic-ide/git``)::
GET /inspect Branch, changes, ahead/behind, branches, worktrees
GET /changes Changed files under a folder, with line counts
GET /diff One file's change against the last commit
GET /overview A workspace's branches with merged-into, PR and CI state
GET /session-status GitHub branch, pull request and CI status per pane
GET /done-gate The agent done gate's last verdict per pane (local files only)
GET /branch/contents A branch's own commits and changed files
GET /branch/diff How a branch changed one file
GET /branch/editors Where a branch's checkout can be opened on this computer
POST /branch/open Open a branch's checkout in the file manager or an editor
GET /github/repos The person's GitHub repositories, for the one-time pick
PUT /github/binding Remember which GitHub repository a workspace folder is
POST /prepare Git half of opening a workspace or an agent
POST /commit Stage everything and commit
POST /push Push the current branch (sets upstream once)
POST /fetch Fetch every remote
POST /pull-request Open a PR with the GitHub CLI
POST /pull Fast-forward a workspace's branch (or rebase it, when asked)
POST /switch Check out another branch in a workspace's folder
POST /branch/push Push a workspace's current branch, or a named one
POST /branch/update Fast-forward a branch to its upstream without switching to it
POST /branch/delete Delete a branch (asks again when its commits exist nowhere else)
POST /branch/pull-request Open a PR with the GitHub connection (or the GitHub CLI)
POST /merge Merge a branch back into its target
POST /worktrees/remove Remove a linked worktree (asks again when dirty)
POST /worktrees/prune Forget worktrees deleted by hand
The older routes take the folder they work on — a workspace folder, a project
path or a worktree. The Git tab's own actions (pull, switch, ``/branch/*``) take
the id of an open workspace instead, so they can only ever act on a repository
the person has open. All of them run git in the anyio threadpool (plain ``def``
handlers), never on the event loop. Loopback-only like the rest of the IDE API.
A failed git call answers 422 with ``{"detail": {"message", "code"}}`` so the
UI can offer the follow-up it has for that code (``dirty`` → "remove anyway?").
"""
from __future__ import annotations
from dataclasses import asdict
from pathlib import Path
from typing import Any
from fastapi import APIRouter, HTTPException, Query, Request
from pydantic import BaseModel, Field
from dodaios.agentic_ide import (
branch_contents,
done_gate_status,
git_changes,
git_ops,
git_overview,
github_link,
github_pulls,
github_status,
)
from dodaios.agentic_ide.git_ops import GitError, PrepareMode
from dodaios.agentic_ide.session import Session, account_home, get_registry
from dodaios.agentic_ide.session_branches import PaneBranchRecord
from dodaios.agentic_ide.thread_folders import allowed_thread_folder
from dodaios.ui.web.surface_security import is_loopback_request
router = APIRouter(prefix="/api/agentic-ide/git", tags=["agentic-ide-git"])
def _open_folders() -> list[str]:
"""Every folder something is running in: each workspace, and each pane's own
checkout (an agent's worktree) — none of them may be removed under it."""
folders: list[str] = []
for session in get_registry().sessions:
folders.append(session.folder)
folders.extend(pane.cwd(session.folder) for pane in session.terminals)
return folders
def _fail(exc: GitError) -> HTTPException:
return HTTPException(status_code=422, detail={"message": str(exc), "code": exc.code})
class FolderRequest(BaseModel):
folder: str = Field(..., min_length=1, description="Folder inside the repository")
class PrepareRequest(FolderRequest):
mode: PrepareMode = "current"
branch: str = Field("", max_length=200)
base: str = Field("", max_length=400, description="Base branch/commit, or worktree path")
class CommitRequest(FolderRequest):
message: str = Field(..., min_length=1, max_length=5000)
class PullRequestRequest(FolderRequest):
title: str = Field("", max_length=300)
draft: bool = False
class WorkspaceRequest(BaseModel):
workspace_id: str = Field(..., min_length=1, description="The open workspace to act in")
class PullBranchRequest(WorkspaceRequest):
rebase: bool = Field(False, description="Rebase local commits onto the remote ones")
class BranchRequest(WorkspaceRequest):
branch: str = Field(..., min_length=1, max_length=300)
class PushBranchRequest(WorkspaceRequest):
branch: str = Field("", max_length=300, description="Branch to push; current if empty")
class DeleteBranchRequest(BranchRequest):
force: bool = Field(False, description="Delete even when its commits exist nowhere else")
remote: bool = Field(False, description="Also delete the branch on the remote")
with_worktree: bool = Field(False, description="Also remove its clean, unused worktree")
class BranchPullRequestRequest(WorkspaceRequest):
branch: str = Field("", max_length=300, description="Branch to open it from; current if empty")
title: str = Field("", max_length=300)
draft: bool = False
class MergeRequest(FolderRequest):
into: str = Field("", max_length=200, description="Target branch; default branch if empty")
class RemoveWorktreeRequest(FolderRequest):
worktree: str = Field(..., min_length=1)
force: bool = False
delete_branch: bool = False
@router.get("/inspect", summary="Git status, branches and worktrees of a folder")
def inspect_folder(folder: str = Query(..., min_length=1)) -> dict:
try:
return git_ops.inspect(folder).to_dict()
except GitError as exc:
raise _fail(exc) from exc
@router.get("/changes", summary="Changed files under a folder, with line counts")
def folder_changes(folder: str = Query(..., min_length=1)) -> dict:
"""What a thread's diff panel lists — the same reading as a workspace's
Changes tab, for a folder no workspace has open (a thread's worktree).
Only a folder inside a connected project or an open workspace is read.
"""
return asdict(git_changes.workspace_changes(_thread_folder(folder)))
def _thread_folder(folder: str) -> Path:
path = allowed_thread_folder(folder)
if path is None:
raise HTTPException(
status_code=403, detail="This folder is not part of a connected project."
)
return path
@router.get("/diff", summary="How one file under a folder differs from the last commit")
def folder_file_diff(
folder: str = Query(..., min_length=1), path: str = Query(..., min_length=1)
) -> dict:
try:
return asdict(git_changes.file_diff(_thread_folder(folder), path))
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from None
@router.get("/overview", summary="Branches of a workspace's repository with PR and CI state")
def workspace_overview(
workspace_id: str = Query(..., min_length=1),
refresh: bool = Query(False, description="Re-read GitHub now instead of the cached answer"),
) -> dict:
"""What the IDE's Git tab lists: every branch, what it is merged into, its
pull requests and its CI — local git always, GitHub through ``gh`` when it
is installed and signed in (cached per repository, see ``git_overview``).
Workspace-scoped on purpose: the folder comes from the open workspace, so
this cannot be pointed at an arbitrary path on the machine.
"""
session = get_registry().get(workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
# The tab polls again soon while GitHub's first read is still loading.
return git_overview.overview(session.folder, refresh=refresh, wait_for_github=False).to_dict()
@router.get("/session-status", summary="Current GitHub branch, pull request and CI status per pane")
def session_github_status(
workspace_id: str = Query(..., min_length=1),
wait: float = Query(
0, ge=0, le=5, description="Seconds to wait for a read already running to finish"
),
instant: bool = Query(
False, description="Answer from the last read at once; the caller asks again"
),
) -> dict:
"""GitHub state for the branch each pane works in, as last read.
With ``instant`` it answers at once from the last read (see
``github_status.board``); ``refreshing`` says a newer answer is on its
way, and ``wait`` holds the answer until that read lands, for at most that
many seconds. A caller that does not ask for ``instant`` gets the answer
once a running read has landed, as before: it neither knows to ask again
soon nor shows an empty answer as "still loading".
"""
if not instant:
wait = _LEGACY_WAIT_S
registry = get_registry()
session = registry.get(workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
# The tabs behind this one are read ahead, and kept recent while this one
# is shown, so a header there has its branch the moment someone switches.
for other in registry.sessions:
if other.id != workspace_id and github_status.due(other.id):
github_status.read_ahead(other.id, _pane_records(other))
return github_status.board(workspace_id, _pane_records(session), wait=wait)
#: How long an answer for a caller that does not ask for ``instant`` waits
#: for a running read: longer than a cold read of a large workspace takes.
_LEGACY_WAIT_S = 15
def _pane_records(session: Session) -> list[PaneBranchRecord]:
"""One record per pane whose checkout is on this computer."""
return [
PaneBranchRecord(
pane=pane.name,
history_id=pane.history_id,
folder=pane.cwd(session.folder),
agent=pane.agent,
session_id=pane.resume.id if pane.resume else "",
home=account_home(pane.agent, pane.account),
created_branch=pane.branch,
started_at=pane.resume.captured_at if pane.resume else 0,
)
for pane in session.terminals
if not pane.computer_id
]
@router.get("/done-gate", summary="The agent done gate's last verdict per pane")
def pane_done_gate(workspace_id: str = Query(..., min_length=1)) -> dict:
"""What ``scripts/agent_done_gate.py`` last said about each pane's session.
Read from the gate's own state files in the repository's git common dir,
never from GitHub, so it is cheap enough to ask on every header poll. A
pane whose verdict is NONE or HOLD, or that has no record, maps to null.
"""
session = get_registry().get(workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
records = [
done_gate_status.PaneGateRecord(
pane=pane.name,
folder=pane.cwd(session.folder),
session_id=pane.resume.id if pane.resume else "",
branch=pane.branch,
)
for pane in session.terminals
if not pane.computer_id
]
return {"panes": done_gate_status.board(records)}
#: The one entry a thread's answers are keyed by, in place of a pane's name.
THREAD_KEY = "thread"
def _thread_folder_and_agent(request: Request, session_id: str) -> tuple[Any, str, str] | None:
"""An IDE thread's session, its folder and its CLI agent; None when it has no local folder."""
from dodaios.agent_chat.catalog import provider_row
from dodaios.ui.web.agent_chat_routes import _service
session = _service(request).store.get_session(session_id)
if session is None or session.surface != "agent":
raise HTTPException(status_code=404, detail="Thread not found.")
folder = allowed_thread_folder(session.cwd) if session.cwd else None
if folder is None:
return None
row = provider_row(session.provider)
return session, str(folder), row.agent if row is not None else ""
@router.get("/thread-status", summary="GitHub branch, pull request and CI status of a thread")
def thread_github_status(
request: Request,
session_id: str = Query(..., min_length=1),
wait: float = Query(
0, ge=0, le=5, description="Seconds to wait for a read already running to finish"
),
instant: bool = Query(
False, description="Answer from the last read at once; the caller asks again"
),
) -> dict:
"""What ``/session-status`` says about a pane, for an IDE thread.
The thread's coding CLI session names the branches it worked on, the same
way a pane's does; the answer has one entry, under ``thread``.
"""
if not instant:
wait = _LEGACY_WAIT_S
found = _thread_folder_and_agent(request, session_id)
if found is None:
return {"panes": {THREAD_KEY: None}}
session, folder, agent = found
record = PaneBranchRecord(
pane=THREAD_KEY,
history_id=session.session_id,
folder=folder,
agent=agent,
session_id=session.vendor_session or "",
home=account_home(agent, session.account_id),
started_at=(session.created_ms or 0) / 1000,
)
return github_status.board(f"thread:{session.session_id}", [record], wait=wait)
@router.get("/thread-done-gate", summary="The agent done gate's last verdict for a thread")
def thread_done_gate(request: Request, session_id: str = Query(..., min_length=1)) -> dict:
"""What ``/done-gate`` says about a pane, for an IDE thread, under ``thread``."""
found = _thread_folder_and_agent(request, session_id)
if found is None:
return {"panes": {THREAD_KEY: None}}
session, folder, _agent = found
record = done_gate_status.PaneGateRecord(
pane=THREAD_KEY, folder=folder, session_id=session.vendor_session or ""
)
return {"panes": done_gate_status.board([record])}
# The editors a branch's checkout is offered in, when installed.
_BRANCH_EDITORS = ("code", "cursor")
def _native_actions(request: Request) -> bool:
"""Opening a folder acts on the screen of the computer running Jarvis."""
return is_loopback_request(request.scope) and bool(
getattr(request.app.state, "native_file_actions", False)
)
def _workspace_folder(workspace_id: str) -> Path:
session = get_registry().get(workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
return Path(session.folder)
@router.get("/branch/contents", summary="A branch's own commits and changed files")
def branch_contents_route(
workspace_id: str = Query(..., min_length=1),
branch: str = Query(..., min_length=1, max_length=300),
base: str = Query("", max_length=300, description="The default branch to compare with"),
remote: bool = Query(False, description="Read origin/<branch> (a GitHub-only branch)"),
) -> dict:
"""What the Git tab shows when a branch row is folded open."""
folder = _workspace_folder(workspace_id)
return branch_contents.branch_contents(folder, branch, base, remote=remote).to_dict()
@router.get("/branch/diff", summary="How a branch changed one file")
def branch_diff_route(
workspace_id: str = Query(..., min_length=1),
branch: str = Query(..., min_length=1, max_length=300),
path: str = Query(..., min_length=1, max_length=1000),
base: str = Query("", max_length=300),
remote: bool = Query(False),
) -> dict:
folder = _workspace_folder(workspace_id)
try:
diff = branch_contents.branch_file_diff(folder, branch, base, path, remote=remote)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from None
return asdict(diff)
@router.get("/branch/editors", summary="Where a branch's checkout can be opened")
def branch_editors(request: Request) -> dict:
"""The file manager and the installed editors; empty on a headless host."""
if not _native_actions(request):
return {"file_manager": False, "editors": []}
from dodaios.ui.web import outputs_routes
labels = dict(outputs_routes._OPENER_EDITORS)
installed = {entry["id"] for entry in outputs_routes._available_openers()}
editors = [{"id": oid, "label": labels[oid]} for oid in _BRANCH_EDITORS if oid in installed]
return {"file_manager": True, "editors": editors}
class OpenBranchRequest(BaseModel):
workspace_id: str = Field(..., min_length=1)
branch: str = Field(..., min_length=1, max_length=300)
target: str = Field(
"folder", min_length=1, max_length=40, description='"folder" or an editor id'
)
@router.post("/branch/open", summary="Open a branch's checkout in the file manager or an editor")
def open_branch(request: Request, req: OpenBranchRequest) -> dict:
"""Open the folder a branch is checked out in.
The folder comes from git's own worktree list for the open workspace, never
from the client, and ``target`` is a closed id — this cannot open an
arbitrary path or launch an arbitrary program.
"""
if not _native_actions(request):
raise HTTPException(status_code=404, detail="Folders can only be opened on this computer.")
session = get_registry().get(req.workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
folder = git_overview.branch_checkout(session.folder, req.branch)
if folder is None:
raise HTTPException(
status_code=404, detail="This branch is not checked out on this computer."
)
from dodaios.platform import open_path
if req.target == "folder":
return {"opened": bool(open_path.open_file(folder)), "path": str(folder)}
if req.target not in _BRANCH_EDITORS:
raise HTTPException(status_code=400, detail="Unknown editor.")
from dodaios.ui.web import outputs_routes
resolved = outputs_routes._resolve_opener(req.target)
if resolved is None:
raise HTTPException(status_code=409, detail="That editor is not installed.")
kind, value = resolved
return {"opened": bool(open_path.open_file_with(folder, kind, value)), "path": str(folder)}
class BindingRequest(BaseModel):
workspace_id: str = Field(..., min_length=1)
repo: str = Field("", max_length=200, description="owner/name; empty forgets the choice")
@router.get("/github/repos", summary="The person's GitHub repositories")
def github_repositories(refresh: bool = Query(False)) -> dict:
"""What the Git tab's one-time "which repository is this folder?" list shows.
Uses the existing GitHub connection (Plugins → GitHub, else the GitHub
CLI's login). ``connected=false`` when there is none — the tab then offers
to connect instead of an empty list.
"""
cred = github_link.credential()
if cred is None:
return {"connected": False, "source": "", "login": "", "repos": [], "reason": ""}
try:
login, repos = github_link.list_repositories(cred, refresh=refresh)
except github_link.GitHubError as exc:
# The GitHub error is returned to the UI as the reason field.
return {
"connected": exc.code != "not_connected",
"source": cred.source,
"login": "",
"repos": [],
"reason": str(exc),
}
return {
"connected": True,
"source": cred.source,
"login": login,
"repos": github_link.repo_dicts(repos),
"reason": "",
}
@router.put("/github/binding", summary="Remember a workspace folder's GitHub repository")
def bind_github_repository(req: BindingRequest) -> dict:
"""Asked once per folder; every worktree of the same repository shares it."""
session = get_registry().get(req.workspace_id)
if session is None:
raise HTTPException(status_code=404, detail="Workspace not found.")
try:
github_link.bind_repository(session.folder, req.repo)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from None
except OSError as exc:
raise HTTPException(status_code=500, detail="The choice could not be saved.") from exc
git_overview.clear_cache()
return {"ok": True, "repo": req.repo.strip()}
@router.post("/prepare", summary="Prepare a checkout for a new workspace or agent")
def prepare_checkout(req: PrepareRequest) -> dict:
try:
return git_ops.prepare(req.folder, req.mode, branch=req.branch, base=req.base).to_dict()
except GitError as exc:
raise _fail(exc) from exc
@router.post("/commit", summary="Stage every change and commit it")
def commit_changes(req: CommitRequest) -> dict:
try:
sha = git_ops.commit_all(req.folder, req.message)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "commit": sha}
@router.post("/push", summary="Push the current branch")
def push_branch(req: FolderRequest) -> dict:
try:
upstream = git_ops.push(req.folder)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "upstream": upstream}
@router.post("/fetch", summary="Fetch every remote")
def fetch_remotes(req: FolderRequest) -> dict:
try:
git_ops.fetch(req.folder)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True}
@router.post("/pull-request", summary="Open a pull request for the current branch")
def open_pull_request(req: PullRequestRequest) -> dict:
try:
url = git_ops.create_pull_request(req.folder, title=req.title, draft=req.draft)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "url": url}
# The Git tab's actions. Each names an open workspace, never a path: the folder
# comes from the workspace, so none of them can be pointed elsewhere on the machine.
@router.post("/pull", summary="Bring a workspace's branch up to date with its remote")
def pull_branch(req: PullBranchRequest) -> dict:
"""Fast-forward only unless ``rebase``; a conflicting rebase is aborted."""
folder = _workspace_folder(req.workspace_id)
try:
message = git_ops.pull(folder, rebase=req.rebase)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "message": message}
@router.post("/switch", summary="Check out another branch in a workspace's folder")
def switch_branch(req: BranchRequest) -> dict:
folder = _workspace_folder(req.workspace_id)
try:
prepared = git_ops.prepare(folder, "switch_branch", branch=req.branch)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "branch": prepared.branch, "message": prepared.message}
@router.post("/branch/push", summary="Push a workspace's current branch, or a named one")
def push_workspace_branch(req: PushBranchRequest) -> dict:
folder = _workspace_folder(req.workspace_id)
try:
upstream = git_ops.push(folder, req.branch)
except GitError as exc:
raise _fail(exc) from exc
git_overview.clear_github(folder)
return {"ok": True, "upstream": upstream}
@router.post("/branch/update", summary="Fast-forward a branch to its upstream")
def update_branch(req: BranchRequest) -> dict:
folder = _workspace_folder(req.workspace_id)
try:
message = git_ops.update_branch(folder, req.branch)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "message": message}
@router.post(
"/branch/delete",
summary="Delete a local branch",
openapi_extra={"x-jarvis-dangerous": True},
)
def delete_branch(req: DeleteBranchRequest) -> dict:
"""Delete a branch whose work is safe elsewhere; ask again otherwise.
"Safe" is decided here, not by the caller: every commit is in another
branch or on a remote that stays, or GitHub merged the pull request that
carried exactly this tip. Anything else answers ``unmerged`` until ``force``.
"""
folder = _workspace_folder(req.workspace_id)
try:
name = git_ops.normalize_branch(req.branch)
tip = git_ops.branch_tip(folder, name)
merged = git_overview.merged_pull_request(folder, name, tip) is not None
message = git_ops.delete_branch(
folder,
name,
force=req.force,
merged=merged,
remote=req.remote,
with_worktree=req.with_worktree,
open_folders=_open_folders(),
)
except GitError as exc:
raise _fail(exc) from exc
if req.remote:
git_overview.clear_github(folder)
return {"ok": True, "message": message}
def _create_pull_request(folder: Path, req: BranchPullRequestRequest) -> str:
"""Push the branch if needed and open its pull request. Returns the URL."""
return github_pulls.open_for_branch(
folder, branch=req.branch, title=req.title, draft=req.draft
)
@router.post("/branch/pull-request", summary="Open a pull request for a workspace's branch")
def open_branch_pull_request(req: BranchPullRequestRequest) -> dict:
folder = _workspace_folder(req.workspace_id)
try:
url = _create_pull_request(folder, req)
except GitError as exc:
raise _fail(exc) from exc
git_overview.clear_github(folder)
return {"ok": True, "url": url}
@router.post("/merge", summary="Merge the current branch back into its target")
def merge_branch(req: MergeRequest) -> dict:
try:
sha = git_ops.merge_back(req.folder, into=req.into)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "commit": sha}
@router.post(
"/worktrees/remove",
summary="Remove a linked git worktree",
openapi_extra={"x-jarvis-dangerous": True},
)
def remove_worktree(req: RemoveWorktreeRequest) -> dict:
if git_ops.worktree_in_use(req.worktree, _open_folders()):
raise HTTPException(
status_code=422,
detail={
"message": "A workspace is still open in this worktree. Close it first.",
"code": "in_use",
},
)
try:
summary = git_ops.remove_worktree(
req.folder, req.worktree, force=req.force, delete_branch=req.delete_branch
)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True, "message": summary}
@router.post("/worktrees/prune", summary="Forget worktrees whose folders are gone")
def prune_worktrees(req: FolderRequest) -> dict:
try:
git_ops.prune_worktrees(req.folder)
except GitError as exc:
raise _fail(exc) from exc
return {"ok": True}