File size: 6,607 Bytes
6daf142
 
 
 
 
 
 
b43ae79
6daf142
 
 
 
 
b43ae79
6daf142
 
b43ae79
 
6daf142
 
 
 
 
 
 
 
 
 
1607c63
6daf142
 
 
 
 
 
1607c63
6daf142
1607c63
6daf142
 
 
 
 
 
 
 
 
 
b43ae79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daf142
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
FastAPI application for the API Contract Validator Environment.

Exposes the ValidatorEnvironment over HTTP and WebSocket endpoints
using ``openenv.core.env_server.http_server.create_app``.

Endpoints created automatically:
    - GET  /        β€” Landing page (this module)
    - POST /reset   β€” Reset the environment
    - POST /step    β€” Execute an action
    - GET  /state   β€” Get current environment state
    - GET  /health  β€” Health check
    - WS   /ws      β€” WebSocket for persistent sessions
    - GET  /docs    β€” Swagger UI (interactive β€” try every endpoint)
"""

from fastapi.responses import HTMLResponse

try:
    from openenv.core.env_server.http_server import create_app
except Exception as exc:
    raise ImportError(
        "openenv is required. Install with: pip install openenv-core"
    ) from exc

try:
    from ..models import ValidatorAction, ValidatorObservation
    from .environment import ValidatorEnvironment
    from .logging_setup import configure_logging
except (ImportError, ModuleNotFoundError):
    import sys
    import os
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    from models import ValidatorAction, ValidatorObservation
    from server.environment import ValidatorEnvironment
    from server.logging_setup import configure_logging

configure_logging()

app = create_app(
    ValidatorEnvironment,
    ValidatorAction,
    ValidatorObservation,
    env_name="api_contract_validator",
    max_concurrent_envs=10,
)


_LANDING_HTML = """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Enterprise Contract Guardian β€” OpenEnv</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
         margin: 0; padding: 2rem; background: #0b1020; color: #e7ecf3; line-height: 1.55; }
  .container { max-width: 760px; margin: 0 auto; }
  h1 { font-size: 1.6rem; margin: 0 0 .25rem; }
  h2 { font-size: 1.05rem; margin: 1.6rem 0 .5rem; color: #9bb0d6;
       text-transform: uppercase; letter-spacing: 0.06em; font-weight: 600; }
  .tag { display: inline-block; padding: 2px 10px; border-radius: 999px;
         background: #16a34a; color: white; font-size: 0.78rem; font-weight: 600;
         margin-left: .4rem; vertical-align: middle; }
  a { color: #6db9ff; text-decoration: none; }
  a:hover { text-decoration: underline; }
  code { background: #1c2540; padding: 2px 6px; border-radius: 4px; font-size: .9em; }
  pre { background: #1c2540; padding: 1rem; border-radius: 8px; overflow-x: auto; font-size: .88em; }
  table { width: 100%; border-collapse: collapse; margin-top: .5rem; }
  td, th { padding: .55rem .75rem; text-align: left;
           border-bottom: 1px solid #1c2540; font-size: .92em; vertical-align: top; }
  th { color: #9bb0d6; font-weight: 600; font-size: .78em;
       text-transform: uppercase; letter-spacing: .04em; }
  .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
  .card { background: #131a35; padding: 1rem 1.2rem; border-radius: 8px;
          border: 1px solid #1c2540; }
  .footer { margin-top: 2rem; color: #7c8db0; font-size: .82em; }
  .pill { display:inline-block; padding:1px 8px; border-radius:6px;
          background:#1c2540; font-size:.78em; color:#9bb0d6; margin-right:4px; }
</style>
</head>
<body>
<div class="container">
  <h1>Enterprise Contract Guardian <span class="tag">RUNNING</span></h1>
  <p>An <b>OpenEnv</b> RL environment that trains agents to detect API contract
  violations, trace downstream blast radius across microservices, and propose
  backward-compatible fixes.</p>
  <p style="color:#9bb0d6;">
    <span class="pill">Theme #3.1</span>
    <span class="pill">Scaler AI Labs Bonus</span>
    <span class="pill">openenv-core 0.2.3</span>
    <span class="pill">9 tasks Β· 3 phases Β· 14 reward signals</span>
  </p>

  <h2>Endpoints</h2>
  <table>
    <tr><th>Method</th><th>Path</th><th>Purpose</th></tr>
    <tr><td>GET</td><td><a href="/health">/health</a></td><td>Liveness check</td></tr>
    <tr><td>GET</td><td><a href="/docs">/docs</a></td>
        <td>Interactive Swagger UI &mdash; click "Try it out" on any endpoint</td></tr>
    <tr><td>POST</td><td><code>/reset</code></td>
        <td>Start a new episode (optional <code>task_name</code>, <code>seed</code>)</td></tr>
    <tr><td>POST</td><td><code>/step</code></td>
        <td>Submit one ValidatorAction</td></tr>
    <tr><td>GET</td><td><code>/state</code></td>
        <td>Current environment state</td></tr>
    <tr><td>WS</td><td><code>/ws</code></td>
        <td>WebSocket session</td></tr>
  </table>

  <h2>Try it from your terminal</h2>
  <pre>HOST=https://pushpam14-api-contract-validator.hf.space

curl $HOST/health
# {"status":"healthy"}

curl -X POST $HOST/reset -H "Content-Type: application/json" \\
     -d '{"task_name":"trace_downstream_blast_radius","seed":1}'</pre>

  <h2>Tasks</h2>
  <div class="grid">
    <div class="card"><b>Phase 1 &mdash; Detection</b><br>
      <code>find_type_mismatches</code><br>
      <code>validate_nested_objects</code><br>
      <code>detect_breaking_changes</code><br>
      <code>validate_response_schema</code><br>
      <code>validate_cross_field_constraints</code><br>
      <code>validate_auth_request</code></div>
    <div class="card"><b>Phase 2 &mdash; Impact Tracing</b><br>
      <code>trace_downstream_blast_radius</code><br><br>
      <b>Phase 3 &mdash; Fix &amp; Verify</b><br>
      <code>propose_backward_compat_fix</code><br>
      <code>multi_service_cascade_fix</code></div>
  </div>

  <h2>Source &amp; documentation</h2>
  <p>
    <a href="https://github.com/kumarpushpam17-personal/Hackathon">GitHub repo</a>
    &nbsp;Β·&nbsp;
    <a href="/docs">Swagger UI (interactive)</a>
    &nbsp;Β·&nbsp;
    <a href="/redoc">ReDoc API spec</a>
  </p>

  <p class="footer">Built with <code>openenv-core</code>, FastAPI, and the
  OpenEnv <i>composable rubric</i> pattern. Reward signal stays independent
  per phase, so per-component contributions are visible in training logs.</p>
</div>
</body>
</html>
"""


@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def root() -> HTMLResponse:
    """Landing page β€” what the HF Space iframe shows by default."""
    return HTMLResponse(content=_LANDING_HTML)


def main(host: str = "0.0.0.0", port: int = 7860) -> None:
    """Entry point for ``uv run server`` or direct execution."""
    import uvicorn

    uvicorn.run(app, host=host, port=port)


if __name__ == "__main__":
    main()