Repository navigation
feat(gateway): add input_required human approval gate for irreversible MCP calls #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 0392a06
fix(goose): depend on amw-sdk>=0.4.0 and reuse its canonical_json (on…
PetrefiedThunder 360d864
fix(goose): real-data-only tests; refuse writes without agent-session…
PetrefiedThunder ed32003
feat(gateway): add input_required human approval gate for irreversibl…
PetrefiedThunder 4e9654b
chore: merge g1/goose-hooks-idempotency into muse/amw-v2-approval-gate
PetrefiedThunder 4e4e0c9
fix(gateway): persist approval gate state, seal tool/session/key, rec…
PetrefiedThunder 48e9263
fix(tests): clean approval_gate_approvals between tests
PetrefiedThunder cc0da45
fix(gateway): check stored approval binding on retry; runtime-generat…
PetrefiedThunder ae49ba4
fix(tests): stable xdist ids, register approval gate table, plain doc…
PetrefiedThunder 20b2ca4
Merge branch 'main' into muse/amw-v2-approval-gate
PetrefiedThunder 3684571
chore(merge): merge main (#215) into approval gate branch
PetrefiedThunder f2f6ce2
chore(merge): merge remote approval-gate branch head
PetrefiedThunder b57a203
fix(gateway): address approval gate review findings
PetrefiedThunder 9a3a477
docs(gateway): drop symbol-style inputResponses references
PetrefiedThunder 7d4d0ea
fix(gateway): enforce approval deadlines, emit consumed receipt, auth…
PetrefiedThunder c102200
fix(gateway): keep CLI missing-key error free of tainted values
PetrefiedThunder ad10285
Merge branch 'main' into muse/amw-v2-approval-gate
PetrefiedThunder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| __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} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
|
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()) | ||
|
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) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.