installgate 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- contracts/README.md +11 -0
- contracts/__init__.py +31 -0
- contracts/contracts.py +260 -0
- contracts/contracts.ts +151 -0
- engine/README.md +36 -0
- engine/__init__.py +1 -0
- engine/cache.py +64 -0
- engine/checks.py +695 -0
- engine/clients/__init__.py +64 -0
- engine/clients/huggingface.py +69 -0
- engine/clients/npm.py +91 -0
- engine/clients/osv.py +91 -0
- engine/clients/pypi.py +197 -0
- engine/data/pypi-top5000-2026-10-27.txt +5011 -0
- engine/evidence.py +55 -0
- engine/parsers.py +216 -0
- engine/policy.py +153 -0
- engine/policy.yaml +92 -0
- engine/runtime_floors.yaml +67 -0
- engine/stage1.py +137 -0
- installgate-0.1.0.dist-info/METADATA +439 -0
- installgate-0.1.0.dist-info/RECORD +58 -0
- installgate-0.1.0.dist-info/WHEEL +4 -0
- installgate-0.1.0.dist-info/entry_points.txt +3 -0
- installgate-0.1.0.dist-info/licenses/LICENSE +202 -0
- mcp_server/README.md +19 -0
- mcp_server/__init__.py +1 -0
- mcp_server/agent.py +61 -0
- mcp_server/api.py +141 -0
- mcp_server/check.py +412 -0
- mcp_server/cli.py +270 -0
- mcp_server/limit.py +74 -0
- mcp_server/render.py +241 -0
- mcp_server/samples.py +53 -0
- mcp_server/server.py +27 -0
- mcp_server/spend.py +90 -0
- remediate/README.md +18 -0
- remediate/__init__.py +1 -0
- remediate/candidates.py +118 -0
- remediate/diff.py +13 -0
- remediate/pinning.py +27 -0
- remediate/sandbox.py +1 -0
- router/README.md +60 -0
- router/__init__.py +1 -0
- router/compare.py +572 -0
- router/prompts/explain.txt +8 -0
- router/prompts/extract.txt +3 -0
- router/prompts/rank_substitute.txt +1 -0
- router/receipt.py +41 -0
- router/smoke.py +49 -0
- router/tavily.py +129 -0
- router/tiers.py +321 -0
- router/token_factory.py +267 -0
- validator/README.md +11 -0
- validator/__init__.py +1 -0
- validator/citations.py +64 -0
- validator/failclosed.py +53 -0
- validator/lattice.py +52 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the Work
|
|
90
|
+
or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
95
|
+
a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by that Contributor, or claims asserted against that
|
|
174
|
+
Contributor, by reason of your accepting any such warranty or
|
|
175
|
+
additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 InstallGate contributors
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
201
|
+
implied. See the License for the specific language governing
|
|
202
|
+
permissions and limitations under the License.
|
mcp_server/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# mcp_server/ — Jenny
|
|
2
|
+
|
|
3
|
+
Same `GateVerdict` for HTTP, CLI, and MCP.
|
|
4
|
+
|
|
5
|
+
| File | Build this |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| `api.py` | FastAPI `POST /check`. Does not read `.env`; keys come from the process environment (`env_file` in `docker-compose.yml`, or `export` before `uvicorn`). |
|
|
8
|
+
| `cli.py` | `ig pip install requests` (or a bare name). Reads `.env`. `ig hook` wraps `pip install`. `installgate` is the same binary. |
|
|
9
|
+
| `server.py` | MCP tool `check_install`, `python -m mcp_server.server` |
|
|
10
|
+
|
|
11
|
+
`check` in `check.py` is the only function that builds a `GateVerdict`. `POST /check`, `ig`, and the MCP tool `check_install` call it and stop. They do not run the command.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv run --no-sync pytest tests/test_gate.py -v
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`POST /check` is limited per visitor (`RATE_LIMIT_PER_MINUTE`). The model budget is `DAILY_SPEND_CAP_USD` for the UTC day; past that, the decision engine still answers. Startup writes the three demo snapshots into the cache when they are missing. `GET /health` is the probe.
|
|
18
|
+
|
|
19
|
+
Tasks: T11, T12, T14.
|
mcp_server/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Jenny. See README.md.
|
mcp_server/agent.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Agent-facing extras on top of GateVerdict. Does not change the frozen contract."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from contracts.contracts import Ecosystem, GateVerdict, Verdict
|
|
8
|
+
from engine.parsers import ParseError, parse_command
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def pinned_command(verdict: GateVerdict) -> str | None:
|
|
12
|
+
"""Copy-paste install for ALLOW. Version pin only — no --hash (deps would fail)."""
|
|
13
|
+
if verdict.final_verdict is not Verdict.ALLOW or not verdict.target.version:
|
|
14
|
+
return None
|
|
15
|
+
target = verdict.target
|
|
16
|
+
if target.ecosystem is Ecosystem.PYPI:
|
|
17
|
+
return f"pip install {target.name}=={target.version}"
|
|
18
|
+
if target.ecosystem is Ecosystem.NPM:
|
|
19
|
+
return f"npm install {target.name}@{target.version}"
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def substitute_command(verdict: GateVerdict) -> str | None:
|
|
24
|
+
rem = verdict.remediation
|
|
25
|
+
if rem is None or rem.status != "VERIFIED_SUBSTITUTE" or rem.candidate is None:
|
|
26
|
+
return None
|
|
27
|
+
candidate = rem.candidate
|
|
28
|
+
if not candidate.version:
|
|
29
|
+
return None
|
|
30
|
+
if candidate.ecosystem is Ecosystem.PYPI:
|
|
31
|
+
return f"pip install {candidate.name}=={candidate.version}"
|
|
32
|
+
if candidate.ecosystem is Ecosystem.NPM:
|
|
33
|
+
return f"npm install {candidate.name}@{candidate.version}"
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def decided_by(verdict: GateVerdict) -> str:
|
|
38
|
+
if verdict.final_verdict == verdict.policy_verdict:
|
|
39
|
+
return "policy"
|
|
40
|
+
return "lattice"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def parse_hint(command: str) -> str | None:
|
|
44
|
+
try:
|
|
45
|
+
targets = parse_command(command)
|
|
46
|
+
except ParseError as exc:
|
|
47
|
+
return str(exc)
|
|
48
|
+
if len(targets) != 1:
|
|
49
|
+
names = ", ".join(item.name for item in targets)
|
|
50
|
+
return f"one package per check, got {len(targets)}: {names}"
|
|
51
|
+
return None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def payload(verdict: GateVerdict, command: str) -> dict[str, object]:
|
|
55
|
+
data: dict[str, object] = json.loads(verdict.model_dump_json())
|
|
56
|
+
data["pinned_command"] = pinned_command(verdict)
|
|
57
|
+
data["substitute_command"] = substitute_command(verdict)
|
|
58
|
+
data["decided_by"] = decided_by(verdict)
|
|
59
|
+
if verdict.failed_closed and not verdict.evidence:
|
|
60
|
+
data["error"] = parse_hint(command) or "check failed closed"
|
|
61
|
+
return data
|
mcp_server/api.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""HTTP entry point (T11, T14). POST /check returns one GateVerdict.
|
|
2
|
+
|
|
3
|
+
POST /check/stream sends the same check as NDJSON stage lines, then the verdict.
|
|
4
|
+
/mcp serves the check_install tool over MCP Streamable HTTP.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import json
|
|
11
|
+
import logging
|
|
12
|
+
import os
|
|
13
|
+
from collections.abc import AsyncIterator
|
|
14
|
+
from contextlib import asynccontextmanager
|
|
15
|
+
|
|
16
|
+
from contracts.contracts import GateVerdict
|
|
17
|
+
from engine.cache import Cache
|
|
18
|
+
from fastapi import FastAPI, Request, Response
|
|
19
|
+
from fastapi.responses import JSONResponse, StreamingResponse
|
|
20
|
+
from pydantic import BaseModel, Field
|
|
21
|
+
from starlette.middleware.base import RequestResponseEndpoint
|
|
22
|
+
from starlette.responses import Response as StarletteResponse
|
|
23
|
+
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
24
|
+
|
|
25
|
+
from mcp_server.check import check
|
|
26
|
+
from mcp_server.limit import client_key, limit_per_minute, limiter
|
|
27
|
+
from mcp_server.samples import warm
|
|
28
|
+
from mcp_server.server import server
|
|
29
|
+
from mcp_server.spend import was_capped
|
|
30
|
+
|
|
31
|
+
_log = logging.getLogger("installgate.api")
|
|
32
|
+
_LIMITED = {"/check", "/check/stream", "/mcp"}
|
|
33
|
+
|
|
34
|
+
_mcp: dict[str, ASGIApp] = {}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def _mcp_asgi(scope: Scope, receive: Receive, send: Send) -> None:
|
|
38
|
+
await _mcp["app"](scope, receive, send)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def configure_logging() -> None:
|
|
42
|
+
"""JSON lines from installgate.* loggers to stderr at LOG_LEVEL. Uvicorn does not do this."""
|
|
43
|
+
level = os.environ.get("LOG_LEVEL", "INFO").strip().upper()
|
|
44
|
+
root = logging.getLogger("installgate")
|
|
45
|
+
root.setLevel(level if level in logging.getLevelNamesMapping() else "INFO")
|
|
46
|
+
# The MCP SDK puts a rich handler on the root logger; one JSON line per event is enough.
|
|
47
|
+
root.propagate = False
|
|
48
|
+
if not root.handlers:
|
|
49
|
+
handler = logging.StreamHandler()
|
|
50
|
+
handler.setFormatter(logging.Formatter("%(message)s"))
|
|
51
|
+
root.addHandler(handler)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@asynccontextmanager
|
|
55
|
+
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
|
|
56
|
+
configure_logging()
|
|
57
|
+
cache = Cache(os.environ.get("CACHE_PATH", "./.cache/installgate.sqlite"))
|
|
58
|
+
try:
|
|
59
|
+
warm(cache)
|
|
60
|
+
finally:
|
|
61
|
+
cache.close()
|
|
62
|
+
# A session manager runs once, so each lifespan gets a new one. Stateless JSON: one
|
|
63
|
+
# POST in, one JSON-RPC response out. host="0.0.0.0" skips the localhost-only Host
|
|
64
|
+
# check, which would reject the UI proxy and hosted judges.
|
|
65
|
+
_mcp["app"] = server.streamable_http_app(
|
|
66
|
+
stateless_http=True, json_response=True, host="0.0.0.0"
|
|
67
|
+
)
|
|
68
|
+
async with server.session_manager.run():
|
|
69
|
+
yield
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
app = FastAPI(title="InstallGate", lifespan=lifespan)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class CheckIn(BaseModel):
|
|
76
|
+
command: str = Field(min_length=1, max_length=4000)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@app.middleware("http")
|
|
80
|
+
async def limit_checks(request: Request, call_next: RequestResponseEndpoint) -> StarletteResponse:
|
|
81
|
+
if request.method == "POST" and request.url.path in _LIMITED:
|
|
82
|
+
if not limiter.allow(client_key(request), limit=limit_per_minute()):
|
|
83
|
+
return JSONResponse(
|
|
84
|
+
{"detail": "Too many checks. Wait a minute."},
|
|
85
|
+
status_code=429,
|
|
86
|
+
)
|
|
87
|
+
return await call_next(request)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@app.get("/health")
|
|
91
|
+
async def health() -> dict[str, bool]:
|
|
92
|
+
return {"ok": True}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@app.post("/check", response_model=GateVerdict)
|
|
96
|
+
async def post_check(body: CheckIn, response: Response) -> GateVerdict:
|
|
97
|
+
verdict = await check(body.command)
|
|
98
|
+
if was_capped():
|
|
99
|
+
response.headers["X-InstallGate-Spend"] = "capped"
|
|
100
|
+
return verdict
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@app.post("/check/stream")
|
|
104
|
+
async def post_check_stream(body: CheckIn) -> StreamingResponse:
|
|
105
|
+
queue: asyncio.Queue[dict[str, object] | None] = asyncio.Queue()
|
|
106
|
+
|
|
107
|
+
async def run() -> GateVerdict:
|
|
108
|
+
try:
|
|
109
|
+
return await check(body.command, on_stage=queue.put_nowait)
|
|
110
|
+
finally:
|
|
111
|
+
queue.put_nowait(None)
|
|
112
|
+
|
|
113
|
+
async def lines() -> AsyncIterator[str]:
|
|
114
|
+
task = asyncio.create_task(run())
|
|
115
|
+
while (event := await queue.get()) is not None:
|
|
116
|
+
yield json.dumps({"type": "stage", **event}) + "\n"
|
|
117
|
+
try:
|
|
118
|
+
verdict = await task
|
|
119
|
+
except Exception:
|
|
120
|
+
_log.exception("stream check failed")
|
|
121
|
+
yield json.dumps({"type": "error", "detail": "check failed; treat as REVIEW"}) + "\n"
|
|
122
|
+
return
|
|
123
|
+
yield (
|
|
124
|
+
json.dumps(
|
|
125
|
+
{
|
|
126
|
+
"type": "verdict",
|
|
127
|
+
"spend_capped": was_capped(),
|
|
128
|
+
"verdict": verdict.model_dump(mode="json"),
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
+ "\n"
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
return StreamingResponse(
|
|
135
|
+
lines(),
|
|
136
|
+
media_type="application/x-ndjson",
|
|
137
|
+
headers={"Cache-Control": "no-cache, no-transform", "X-Accel-Buffering": "no"},
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
app.mount("/", _mcp_asgi)
|