Repository navigation
Expand file tree
/
Copy pathruntime.py
More file actions
308 lines (282 loc) · 12.2 KB
/
Copy pathruntime.py
File metadata and controls
308 lines (282 loc) · 12.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""Authenticated lifecycle endpoints attached to the existing CLI server."""
from __future__ import annotations
import asyncio
import os
import subprocess
import sys
import time
from fastapi import Request
from fastapi.responses import JSONResponse
from .catalog import Catalog
from .contract import CONTROL_VERSION
from .operations import Operations
from .settings import ApplicationSettings
from . import registry
from .storage import WorkspaceError
class Runtime:
def __init__(self, server, *, settings: ApplicationSettings | None = None):
self.server = server
self.settings = settings or ApplicationSettings()
self._catalog = None
self.workspace = None
self.operations = Operations()
self.managed = (
os.environ.get(self.settings.environment_prefix + "_MANAGED") == "1"
)
self.started = time.time()
self.stopping = False
self.close_failed = False
self.active_calls = 0
self.browser_opened_at = 0.0
self.browser_lock = asyncio.Lock()
self.sessions = []
@property
def catalog(self):
from ..web.security import supports_private_credentials
if not supports_private_credentials():
raise WorkspaceError(
"unsupported_platform",
"Managed workspace history requires tested owner-only storage, which is not yet available on this platform.",
)
if self._catalog is None:
self._catalog = Catalog(self.settings)
return self._catalog
def identity(self):
s = self.server
mcp = s._mcp_status()
return {
"application": self.settings.product,
"instanceId": s.access.binding,
"workspaceId": self.workspace["workspaceId"] if self.workspace else None,
"name": self.workspace["name"] if self.workspace else None,
"databasePath": s.duckdb_database,
**(
{
"profile": s.capability_profile,
"executionMode": s.execution_mode,
"embeddedAiEnabled": s.execution_mode == "embedded",
}
if self.settings.profiles
else {}
),
"ownership": "managed" if self.managed else "manual",
"apiUrl": s._ui_url(),
"browserUrl": s.external_url or s._ui_url() if s.serve_ui else None,
"mcpEnabled": mcp["enabled"],
"mcpUrl": s._mcp_url() if mcp["enabled"] else None,
"toolVersion": self.settings.contract["version"],
"toolHash": self.settings.tool_hash,
"controlVersion": CONTROL_VERSION,
"lifecycle": "stopping" if self.stopping else "running",
"readiness": "ready"
if mcp["enabled"] and mcp["bridge"]["status"] == "ready"
else "waiting_for_browser"
if mcp["enabled"]
else "mcp_unavailable",
"startedAt": self.started,
}
async def publish(self):
s = self.server
self.workspace = await asyncio.to_thread(
self.catalog.register,
s.duckdb_database,
profile=s.capability_profile if self.settings.profiles else None,
opened=True,
)
registry.publish(
{
**self.identity(),
"pid": os.getpid(),
"processMarker": registry.process_marker(os.getpid()),
"credentialFile": str(s.credential_file.path),
},
settings=self.settings,
)
def routes(self, app):
s = self.server
@app.exception_handler(WorkspaceError)
async def workspace_error(_request, exc):
return JSONResponse(exc.result)
def authorize(request, operation="control", *, native=False):
caller = s.security.authorize(request.headers, operation)
if native and caller.kind not in {"native", "connector"}:
from sqlrooms.server.access import AccessDenied
raise AccessDenied("forbidden")
return caller
@app.get("/api/agent/identity")
async def identity(request: Request):
authorize(request, "read")
return self.identity()
@app.post("/api/agent/session")
async def session(request: Request):
caller = authorize(request, native=True)
if caller.kind != "native":
raise WorkspaceError(
"forbidden",
"Only a native handoff can create connector credentials.",
)
now = time.monotonic()
self.sessions = [(t, at) for t, at in self.sessions if now - at < 3600]
if len(self.sessions) >= 128:
raise WorkspaceError(
"sessions_busy",
"Too many connector sessions; restart unused connectors.",
)
token = s.access.issue(
"connector", frozenset({"read", "control", "mcp", "bootstrap"}), 3600
)
self.sessions.append((token, now))
return {"token": token, "instanceId": s.access.binding}
@app.post("/api/agent/cancel")
async def cancel(request: Request):
caller = authorize(request, native=True)
body = await request.json()
return self.operations.cancel(caller, body.get("operationId"))
@app.post("/api/agent/operation")
async def operation(request: Request):
caller = authorize(request, native=True)
body = await request.json()
return self.operations.status(caller, body.get("operationId"))
@app.post("/api/agent/browser")
async def browser(request: Request):
authorize(request, "bootstrap", native=True)
async with self.browser_lock:
if self.stopping:
raise WorkspaceError("workspace_busy", "Workspace is closing.")
status = self.identity()
body = await request.json()
if not s.serve_ui:
raise WorkspaceError(
"ui_unavailable", "This server was started without a UI."
)
# A recovery link is a short-lived, single-use ticket, never a
# reusable credential. It is returned only to an authorized opener.
launch_url = (
s._launch_url()
if status["readiness"] != "ready"
else status["browserUrl"]
)
if (
body.get("openBrowser", True)
and status["readiness"] != "ready"
and time.monotonic() - self.browser_opened_at > 15
):
self.browser_opened_at = time.monotonic()
import webbrowser
try:
opened = await asyncio.to_thread(webbrowser.open, launch_url)
except Exception:
opened = False
status["browserOpened"] = opened
status["launchUrl"] = launch_url
return status
@app.post("/api/agent/close")
async def close(request: Request):
authorize(request, native=True)
if not self.managed:
raise WorkspaceError(
"manual_instance",
"Flush and close this manually launched workspace in its terminal.",
)
if (
(self.stopping and not self.close_failed)
or self.active_calls
or self.operations.active
or s.mcp_broker.status()["pendingRequests"]
):
raise WorkspaceError(
"workspace_busy",
"Wait for active workspace operations before closing.",
)
self.stopping = True
retrying_persistence = self.close_failed
self.close_failed = False
result = {"ok": True, "previouslyConfirmed": True}
if not retrying_persistence:
try:
result = await s.mcp_broker.request("workspace.flush", timeout=15)
if not isinstance(result, dict) or not result.get("ok"):
raise WorkspaceError(
"flush_failed",
"The browser could not confirm its final save. The server remains running.",
)
except BaseException as exc:
self.stopping = False
try:
await s.mcp_broker.request("workspace.resume", timeout=2)
except Exception:
pass
from ..web.mcp_bridge import McpBridgeError
if isinstance(exc, McpBridgeError):
raise WorkspaceError(
exc.code,
"The owning browser could not confirm its final save. Reconnect it and retry close; the server remains running.",
) from exc
raise
try:
await s._app_resources.close()
except (Exception, asyncio.CancelledError) as exc:
self.close_failed = True
if isinstance(exc, asyncio.CancelledError):
raise
raise WorkspaceError(
"persistence_failed",
"Database shutdown could not confirm persistence. Admission remains stopped; resolve the storage problem and retry close. No successful close was recorded.",
) from exc
self.close_failed = False
# Reply only after persistence, before stopping the shared listener.
asyncio.get_running_loop().call_later(
0.2, setattr, s._http_server, "should_exit", True
)
return {
"ok": True,
"application": self.settings.product,
"instanceId": s.access.binding,
"lifecycle": "stopping",
"flush": result,
}
@app.get("/api/workspaces")
async def recent(request: Request):
authorize(request, "read")
from .manager import Manager
return await asyncio.to_thread(
Manager(self.catalog).list,
refresh=request.query_params.get("refresh") == "true",
offset=int(request.query_params.get("offset", "0")),
)
@app.post("/api/workspaces/{action}")
async def catalog_action(action: str, request: Request):
authorize(request)
from .manager import Manager
body = await request.json()
manager = Manager(self.catalog)
if action == "open":
return await asyncio.to_thread(manager.open, **body)
if action == "locate":
return await asyncio.to_thread(manager.locate, **body)
if action == "forget":
return await asyncio.to_thread(self.catalog.forget, body["workspaceId"])
if action == "rename":
renamed = await asyncio.to_thread(
self.catalog.rename, body["workspaceId"], body["name"]
)
if (
self.workspace
and renamed["workspaceId"] == self.workspace["workspaceId"]
):
self.workspace = renamed
return renamed
if action == "reveal":
if not s.db_path:
raise WorkspaceError(
"temporary_workspace",
"Temporary workspaces have no project folder.",
)
command = (
["/usr/bin/open", "-R", str(s.db_path)]
if sys.platform == "darwin"
else ["xdg-open", str(s.db_path.parent)]
)
await asyncio.to_thread(subprocess.run, command, check=True, timeout=5)
return {"ok": True}
raise WorkspaceError("unknown_action", "Unknown catalog operation.")