Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e76f5f1
feat(goose): AMW StreamableHttp proxy, PreToolUse permit hook, fallba…
PetrefiedThunder Oct 24, 2026
0392a06
fix(goose): depend on amw-sdk>=0.4.0 and reuse its canonical_json (on…
PetrefiedThunder Oct 24, 2026
360d864
fix(goose): real-data-only tests; refuse writes without agent-session…
PetrefiedThunder Oct 24, 2026
ed32003
feat(gateway): add input_required human approval gate for irreversibl…
PetrefiedThunder Oct 24, 2026
4e9654b
chore: merge g1/goose-hooks-idempotency into muse/amw-v2-approval-gate
PetrefiedThunder Oct 24, 2026
4e4e0c9
fix(gateway): persist approval gate state, seal tool/session/key, rec…
PetrefiedThunder Oct 24, 2026
48e9263
fix(tests): clean approval_gate_approvals between tests
PetrefiedThunder Oct 24, 2026
cc0da45
fix(gateway): check stored approval binding on retry; runtime-generat…
PetrefiedThunder Oct 24, 2026
ae49ba4
fix(tests): stable xdist ids, register approval gate table, plain doc…
PetrefiedThunder Oct 24, 2026
20b2ca4
Merge branch 'main' into muse/amw-v2-approval-gate
PetrefiedThunder Oct 25, 2026
3684571
chore(merge): merge main (#215) into approval gate branch
PetrefiedThunder Oct 25, 2026
f2f6ce2
chore(merge): merge remote approval-gate branch head
PetrefiedThunder Oct 25, 2026
b57a203
fix(gateway): address approval gate review findings
PetrefiedThunder Oct 25, 2026
9a3a477
docs(gateway): drop symbol-style inputResponses references
PetrefiedThunder Oct 25, 2026
7d4d0ea
fix(gateway): enforce approval deadlines, emit consumed receipt, auth…
PetrefiedThunder Oct 25, 2026
c102200
fix(gateway): keep CLI missing-key error free of tainted values
PetrefiedThunder Oct 25, 2026
ad10285
Merge branch 'main' into muse/amw-v2-approval-gate
PetrefiedThunder Oct 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions gateway/app/db/approval_gate_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Durable rows for the AMW V2 human approval gate (SEP-2322).

One row per paused irreversible MCP tool call. The sealed ``requestState`` the
client retries with is authenticated by HMAC, but the decision and the
single-use consumption must survive a gateway restart, so they live here
instead of only in memory. See ``app/services/approval_gate.py`` and
migration ``045_approval_gate_approvals``.
"""

from __future__ import annotations

from datetime import datetime
from typing import Optional

from sqlalchemy import CheckConstraint
from sqlmodel import Field, SQLModel

from app.core.time import utc_now
from app.db.types import NaiveUTCDateTime

APPROVAL_GATE_STATUS_PENDING = "pending"
APPROVAL_GATE_STATUS_APPROVED = "approved"
APPROVAL_GATE_STATUS_DENIED = "denied"
APPROVAL_GATE_STATUS_EXPIRED = "expired"


class ApprovalGateModel(SQLModel, table=True):
"""One paused tool call awaiting (or holding) a human decision.

``nonce`` is the primary key, so it is unique by definition: one row per
pause, and the atomic consume (``UPDATE ... WHERE status='approved' AND
consumed_at IS NULL``, rowcount checked) makes the approval single-use
even across a restart or concurrent retries. Raw arguments are never
stored, only their canonical hash.
"""

__tablename__ = "approval_gate_approvals"
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
__table_args__ = (
CheckConstraint(
"status IN ('pending', 'approved', 'denied', 'expired')",
name="ck_approval_gate_approvals_status",
),
)

nonce: str = Field(primary_key=True, max_length=64)
idempotency_key: str = Field(max_length=128, index=True)
args_hash: str = Field(max_length=128)
principal: str = Field(max_length=255)
tool: str = Field(max_length=128)
session_id: str = Field(max_length=128)
status: str = Field(default="pending", max_length=16, index=True)
created_at: datetime = Field(sa_type=NaiveUTCDateTime, default_factory=utc_now)
deadline: datetime = Field(sa_type=NaiveUTCDateTime)
decided_at: Optional[datetime] = Field(sa_type=NaiveUTCDateTime, default=None)
consumed_at: Optional[datetime] = Field(sa_type=NaiveUTCDateTime, default=None)

model_config = {"arbitrary_types_allowed": True}
105 changes: 105 additions & 0 deletions gateway/app/routers/approvals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""Local-only human approval endpoints (AMW V2).

A human on the gateway host approves or denies paused irreversible tool calls
here (or via ``gateway/scripts/amw_approvals.py``). Every endpoint rejects
non-loopback clients with 403 and requires a bootstrap admin (operator) API
key. This router is intentionally not mounted in ``main.py`` yet; mounting
lands with the ``mcp_standard.py`` hook in a later PR.
"""

from __future__ import annotations

import time

from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel

from app.core.auth import AuthContext, get_auth_context
from app.services.approval_gate import ApprovalStore, DbApprovalStore

router = APIRouter(prefix="/v1/approvals", tags=["Approvals"])

LOOPBACK_HOSTS = frozenset({"127.0.0.1", "::1", "localhost"})


def require_loopback(request: Request) -> None:
"""Allow only clients on the gateway host itself."""
host = request.client.host if request.client else ""
if host not in LOOPBACK_HOSTS:
raise HTTPException(status_code=403, detail="approvals are local-only")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved


_store: ApprovalStore = DbApprovalStore()


def get_approval_store() -> ApprovalStore:
return _store


def set_approval_store_for_tests(store: ApprovalStore) -> None:
global _store
_store = store


class PendingApproval(BaseModel):
nonce: str
tool: str
principal: str
args_hash: str
created_at: float
deadline: float


class PendingApprovalsResponse(BaseModel):
approvals: list[PendingApproval]


class DecisionResponse(BaseModel):
nonce: str
status: str


@router.get("/pending", response_model=PendingApprovalsResponse)
async def list_pending(
_local: None = Depends(require_loopback),
auth: AuthContext = Depends(get_auth_context),
) -> PendingApprovalsResponse:
auth.require_bootstrap_admin()
approvals = [
PendingApproval(
nonce=record.nonce,
tool=record.tool,
principal=record.principal,
args_hash=record.args_hash,
created_at=record.created_at,
deadline=record.deadline,
)
for record in await get_approval_store().alist_pending(now=time.time())
]
return PendingApprovalsResponse(approvals=approvals)


@router.post("/{nonce}/approve", response_model=DecisionResponse)
async def approve(
nonce: str,
_local: None = Depends(require_loopback),
auth: AuthContext = Depends(get_auth_context),
) -> DecisionResponse:
auth.require_bootstrap_admin()
record = await get_approval_store().adecide(nonce, True, now=time.time())
if record is None:
raise HTTPException(status_code=404, detail="approval_not_found")
return DecisionResponse(nonce=record.nonce, status=record.status)


@router.post("/{nonce}/deny", response_model=DecisionResponse)
async def deny(
nonce: str,
_local: None = Depends(require_loopback),
auth: AuthContext = Depends(get_auth_context),
) -> DecisionResponse:
auth.require_bootstrap_admin()
record = await get_approval_store().adecide(nonce, False, now=time.time())
Comment thread
PetrefiedThunder marked this conversation as resolved.
Resolved
if record is None:
raise HTTPException(status_code=404, detail="approval_not_found")
return DecisionResponse(nonce=record.nonce, status=record.status)
Loading
Loading