continuum-task-server-bundle 1.0.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.
- continuum_task_server_bundle/__init__.py +1 -0
- continuum_task_server_bundle/daytona/__init__.py +4 -0
- continuum_task_server_bundle/daytona/common.py +534 -0
- continuum_task_server_bundle/daytona/create_sandbox.py +166 -0
- continuum_task_server_bundle/daytona/dispatch.py +125 -0
- continuum_task_server_bundle/daytona/execute_sandbox.py +243 -0
- continuum_task_server_bundle/daytona/filesystem.py +330 -0
- continuum_task_server_bundle/daytona/fs_edit.py +127 -0
- continuum_task_server_bundle/daytona/git_ops.py +585 -0
- continuum_task_server_bundle/daytona/github_app.py +207 -0
- continuum_task_server_bundle/daytona/manage_sandbox.py +104 -0
- continuum_task_server_bundle/daytona/plugin.py +18 -0
- continuum_task_server_bundle/daytona/s3_transfer.py +207 -0
- continuum_task_server_bundle/discord/__init__.py +4 -0
- continuum_task_server_bundle/discord/attachment_registry_db.py +127 -0
- continuum_task_server_bundle/discord/attachment_resolve.py +213 -0
- continuum_task_server_bundle/discord/await_cleanup.py +105 -0
- continuum_task_server_bundle/discord/discord_ready.py +39 -0
- continuum_task_server_bundle/discord/message_await_apply.py +143 -0
- continuum_task_server_bundle/discord/message_await_db.py +197 -0
- continuum_task_server_bundle/discord/message_await_inbound.py +54 -0
- continuum_task_server_bundle/discord/message_await_logic.py +133 -0
- continuum_task_server_bundle/discord/message_await_poller.py +122 -0
- continuum_task_server_bundle/discord/message_chunking.py +45 -0
- continuum_task_server_bundle/discord/methods.py +408 -0
- continuum_task_server_bundle/discord/plugin.py +105 -0
- continuum_task_server_bundle/discord/reaction_await_apply.py +238 -0
- continuum_task_server_bundle/discord/reaction_await_db.py +174 -0
- continuum_task_server_bundle/discord/reaction_await_inbound.py +47 -0
- continuum_task_server_bundle/discord/reaction_await_logic.py +118 -0
- continuum_task_server_bundle/discord/reaction_await_poller.py +131 -0
- continuum_task_server_bundle/discord/s3_attachment_config.py +25 -0
- continuum_task_server_bundle/discord/s3_message_attachments.py +139 -0
- continuum_task_server_bundle/discord/sql/001_message_awaits.sql +112 -0
- continuum_task_server_bundle/discord/sql/002_reaction_awaits.sql +61 -0
- continuum_task_server_bundle/github/__init__.py +4 -0
- continuum_task_server_bundle/github/awaits/__init__.py +1 -0
- continuum_task_server_bundle/github/awaits/apply.py +92 -0
- continuum_task_server_bundle/github/awaits/cleanup.py +94 -0
- continuum_task_server_bundle/github/awaits/db.py +220 -0
- continuum_task_server_bundle/github/awaits/evaluators.py +325 -0
- continuum_task_server_bundle/github/awaits/fetch.py +192 -0
- continuum_task_server_bundle/github/awaits/poller.py +82 -0
- continuum_task_server_bundle/github/awaits/register.py +37 -0
- continuum_task_server_bundle/github/ci_status.py +70 -0
- continuum_task_server_bundle/github/comment_normalize.py +76 -0
- continuum_task_server_bundle/github/errors.py +52 -0
- continuum_task_server_bundle/github/github_app.py +291 -0
- continuum_task_server_bundle/github/github_http.py +377 -0
- continuum_task_server_bundle/github/methods/__init__.py +90 -0
- continuum_task_server_bundle/github/methods/_common.py +183 -0
- continuum_task_server_bundle/github/methods/awaits.py +107 -0
- continuum_task_server_bundle/github/methods/ci.py +158 -0
- continuum_task_server_bundle/github/methods/comments.py +214 -0
- continuum_task_server_bundle/github/methods/credentials.py +59 -0
- continuum_task_server_bundle/github/methods/issues.py +131 -0
- continuum_task_server_bundle/github/methods/pulls.py +329 -0
- continuum_task_server_bundle/github/methods/repos.py +424 -0
- continuum_task_server_bundle/github/methods/search.py +109 -0
- continuum_task_server_bundle/github/plugin.py +91 -0
- continuum_task_server_bundle/github/sql/001_github_awaits.sql +65 -0
- continuum_task_server_bundle/github/webhook_server.py +126 -0
- continuum_task_server_bundle/jira/__init__.py +4 -0
- continuum_task_server_bundle/jira/await_poller.py +236 -0
- continuum_task_server_bundle/jira/await_store.py +271 -0
- continuum_task_server_bundle/jira/config.py +68 -0
- continuum_task_server_bundle/jira/jira_client.py +382 -0
- continuum_task_server_bundle/jira/methods.py +246 -0
- continuum_task_server_bundle/jira/plugin.py +86 -0
- continuum_task_server_bundle/jira/sql/001_jira_issue_comment_waits.sql +19 -0
- continuum_task_server_bundle/jira/webhook.py +70 -0
- continuum_task_server_bundle/openrouter/__init__.py +4 -0
- continuum_task_server_bundle/openrouter/chat_service.py +1037 -0
- continuum_task_server_bundle/openrouter/chat_store.py +849 -0
- continuum_task_server_bundle/openrouter/environment.py +29 -0
- continuum_task_server_bundle/openrouter/handler.py +59 -0
- continuum_task_server_bundle/openrouter/image_context.py +207 -0
- continuum_task_server_bundle/openrouter/openrouter_client.py +324 -0
- continuum_task_server_bundle/openrouter/plugin.py +37 -0
- continuum_task_server_bundle/openrouter/prompt_projection.py +417 -0
- continuum_task_server_bundle/openrouter/sql/chat.sql +50 -0
- continuum_task_server_bundle/openrouter/sql/migrate_prune_then_chunk_v1.sql +11 -0
- continuum_task_server_bundle/roblox/__init__.py +4 -0
- continuum_task_server_bundle/roblox/open_cloud.py +335 -0
- continuum_task_server_bundle/roblox/plugin.py +30 -0
- continuum_task_server_bundle/roblox/storage_s3.py +55 -0
- continuum_task_server_bundle-1.0.0.dist-info/METADATA +109 -0
- continuum_task_server_bundle-1.0.0.dist-info/RECORD +90 -0
- continuum_task_server_bundle-1.0.0.dist-info/WHEEL +5 -0
- continuum_task_server_bundle-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Composable Continuum task-server integrations."""
|
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from typing import Any, TypeVar
|
|
7
|
+
|
|
8
|
+
from daytona import (
|
|
9
|
+
Daytona,
|
|
10
|
+
DaytonaAuthenticationError,
|
|
11
|
+
DaytonaAuthorizationError,
|
|
12
|
+
DaytonaBadRequestError,
|
|
13
|
+
DaytonaConflictError,
|
|
14
|
+
DaytonaConnectionError,
|
|
15
|
+
DaytonaConnectionTimeoutError,
|
|
16
|
+
DaytonaError,
|
|
17
|
+
DaytonaFileNotFoundError,
|
|
18
|
+
DaytonaForbiddenError,
|
|
19
|
+
DaytonaInvalidFilePathError,
|
|
20
|
+
DaytonaNotFoundError,
|
|
21
|
+
DaytonaProcessExecutionTimeoutError,
|
|
22
|
+
DaytonaProcessNotFoundError,
|
|
23
|
+
DaytonaTimeoutError,
|
|
24
|
+
DaytonaValidationError,
|
|
25
|
+
Sandbox,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
T = TypeVar("T")
|
|
29
|
+
|
|
30
|
+
DEFAULT_LIMITS = {
|
|
31
|
+
"max_output_bytes": 65536,
|
|
32
|
+
"max_output_lines": 200,
|
|
33
|
+
"max_file_bytes": 65536,
|
|
34
|
+
"max_edit_bytes": 4 * 1024 * 1024,
|
|
35
|
+
"max_file_lines": 200,
|
|
36
|
+
"max_write_bytes": 65536,
|
|
37
|
+
"max_list_entries": 200,
|
|
38
|
+
"max_search_results": 100,
|
|
39
|
+
"max_list_depth": 3,
|
|
40
|
+
"max_s3_bytes": 10 * 1024 * 1024,
|
|
41
|
+
"max_exec_timeout": 300,
|
|
42
|
+
"max_create_timeout": 300,
|
|
43
|
+
"max_lifecycle_timeout": 120,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
ENV_LIMITS = {
|
|
47
|
+
"max_output_bytes": "DAYTONA_MAX_OUTPUT_BYTES",
|
|
48
|
+
"max_output_lines": "DAYTONA_MAX_OUTPUT_LINES",
|
|
49
|
+
"max_file_bytes": "DAYTONA_MAX_FILE_BYTES",
|
|
50
|
+
"max_edit_bytes": "DAYTONA_MAX_EDIT_BYTES",
|
|
51
|
+
"max_file_lines": "DAYTONA_MAX_FILE_LINES",
|
|
52
|
+
"max_write_bytes": "DAYTONA_MAX_WRITE_BYTES",
|
|
53
|
+
"max_list_entries": "DAYTONA_MAX_LIST_ENTRIES",
|
|
54
|
+
"max_search_results": "DAYTONA_MAX_SEARCH_RESULTS",
|
|
55
|
+
"max_list_depth": "DAYTONA_MAX_LIST_DEPTH",
|
|
56
|
+
"max_s3_bytes": "DAYTONA_MAX_S3_BYTES",
|
|
57
|
+
"max_exec_timeout": "DAYTONA_MAX_EXEC_TIMEOUT",
|
|
58
|
+
"max_create_timeout": "DAYTONA_MAX_CREATE_TIMEOUT",
|
|
59
|
+
"max_lifecycle_timeout": "DAYTONA_MAX_LIFECYCLE_TIMEOUT",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
SANDBOX_PUBLIC_FIELDS = (
|
|
63
|
+
"id",
|
|
64
|
+
"name",
|
|
65
|
+
"state",
|
|
66
|
+
"desired_state",
|
|
67
|
+
"snapshot",
|
|
68
|
+
"user",
|
|
69
|
+
"labels",
|
|
70
|
+
"public",
|
|
71
|
+
"cpu",
|
|
72
|
+
"gpu",
|
|
73
|
+
"gpu_type",
|
|
74
|
+
"memory",
|
|
75
|
+
"disk",
|
|
76
|
+
"target",
|
|
77
|
+
"error_reason",
|
|
78
|
+
"spot",
|
|
79
|
+
"created_at",
|
|
80
|
+
"updated_at",
|
|
81
|
+
"last_activity_at",
|
|
82
|
+
"auto_stop_interval",
|
|
83
|
+
"auto_pause_interval",
|
|
84
|
+
"auto_archive_interval",
|
|
85
|
+
"auto_delete_interval",
|
|
86
|
+
"linked_sandbox_id",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
UNSTARTABLE_STATES = {
|
|
90
|
+
"destroyed",
|
|
91
|
+
"destroying",
|
|
92
|
+
"error",
|
|
93
|
+
"build_failed",
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# Client-side HTTP deadlines. The SDK default read timeout is unbounded.
|
|
97
|
+
REQUEST_TIMEOUT_RUNNING_SECONDS = 10
|
|
98
|
+
REQUEST_TIMEOUT_STOPPED_SECONDS = 30
|
|
99
|
+
REQUEST_TIMEOUT_ARCHIVED_SECONDS = 120
|
|
100
|
+
SANDBOX_LOOKUP_TIMEOUT_SECONDS = 30
|
|
101
|
+
DAYTONA_RETRY_ATTEMPTS = 3
|
|
102
|
+
DAYTONA_RETRY_BASE_SECONDS = 0.5
|
|
103
|
+
DAYTONA_TIMEOUT_MESSAGE = "Daytona operation timed out"
|
|
104
|
+
_RUNNING_STATES = {"started", "running"}
|
|
105
|
+
|
|
106
|
+
_SECRET_RE = re.compile(
|
|
107
|
+
r"(?i)(aws_secret_access_key|aws_access_key_id|secret_access_key|api[_-]?key|"
|
|
108
|
+
r"authorization|bearer|DAYTONA_API_KEY)\s*[:=]\s*\S+"
|
|
109
|
+
)
|
|
110
|
+
_AKIA_RE = re.compile(r"AKIA[0-9A-Z]{16}")
|
|
111
|
+
ENV_KEY_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class HandlerError(Exception):
|
|
115
|
+
def __init__(self, message: str, code: str = "validation_error"):
|
|
116
|
+
super().__init__(message)
|
|
117
|
+
self.message = message
|
|
118
|
+
self.code = code
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def sanitize(message: str) -> str:
|
|
122
|
+
text = message or ""
|
|
123
|
+
text = _SECRET_RE.sub(r"\1=[redacted]", text)
|
|
124
|
+
return _AKIA_RE.sub("[redacted]", text)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def success(data: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
128
|
+
payload: dict[str, Any] = {"success": True}
|
|
129
|
+
if data is not None:
|
|
130
|
+
payload["data"] = data
|
|
131
|
+
return payload
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def error_response(err: Exception) -> dict[str, Any]:
|
|
135
|
+
mapped = err if isinstance(err, HandlerError) else map_exception(err)
|
|
136
|
+
return {
|
|
137
|
+
"success": False,
|
|
138
|
+
"error": {
|
|
139
|
+
"code": mapped.code,
|
|
140
|
+
"message": sanitize(mapped.message),
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def map_exception(err: Exception) -> HandlerError:
|
|
146
|
+
if isinstance(err, HandlerError):
|
|
147
|
+
return err
|
|
148
|
+
if isinstance(err, DaytonaError):
|
|
149
|
+
return map_daytona(err)
|
|
150
|
+
return HandlerError(sanitize(str(err) or err.__class__.__name__), "internal_error")
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def map_daytona(exc: DaytonaError) -> HandlerError:
|
|
154
|
+
message = sanitize(str(exc) or exc.__class__.__name__)
|
|
155
|
+
if isinstance(
|
|
156
|
+
exc,
|
|
157
|
+
(DaytonaNotFoundError, DaytonaFileNotFoundError, DaytonaProcessNotFoundError),
|
|
158
|
+
):
|
|
159
|
+
return HandlerError(message, "not_found")
|
|
160
|
+
if isinstance(
|
|
161
|
+
exc,
|
|
162
|
+
(
|
|
163
|
+
DaytonaTimeoutError,
|
|
164
|
+
DaytonaProcessExecutionTimeoutError,
|
|
165
|
+
DaytonaConnectionTimeoutError,
|
|
166
|
+
),
|
|
167
|
+
):
|
|
168
|
+
return HandlerError(message, "timeout")
|
|
169
|
+
if isinstance(exc, DaytonaConnectionError):
|
|
170
|
+
return HandlerError(message, "daytona_error")
|
|
171
|
+
if isinstance(exc, DaytonaConflictError):
|
|
172
|
+
return HandlerError(message, "conflict")
|
|
173
|
+
if isinstance(
|
|
174
|
+
exc,
|
|
175
|
+
(DaytonaValidationError, DaytonaBadRequestError, DaytonaInvalidFilePathError),
|
|
176
|
+
):
|
|
177
|
+
return HandlerError(message, "validation_error")
|
|
178
|
+
if isinstance(
|
|
179
|
+
exc,
|
|
180
|
+
(DaytonaAuthenticationError, DaytonaAuthorizationError, DaytonaForbiddenError),
|
|
181
|
+
):
|
|
182
|
+
return HandlerError(message, "forbidden")
|
|
183
|
+
return HandlerError(message, "daytona_error")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def env_int(name: str, default: int) -> int:
|
|
187
|
+
raw = os.getenv(name)
|
|
188
|
+
if raw is None or raw == "":
|
|
189
|
+
return default
|
|
190
|
+
try:
|
|
191
|
+
value = int(raw)
|
|
192
|
+
except ValueError:
|
|
193
|
+
return default
|
|
194
|
+
if value < 1:
|
|
195
|
+
return default
|
|
196
|
+
return value
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def ceiling_limits() -> dict[str, int]:
|
|
200
|
+
return {
|
|
201
|
+
key: env_int(ENV_LIMITS[key], default)
|
|
202
|
+
for key, default in DEFAULT_LIMITS.items()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def effective_limit(definition: dict, key: str) -> int:
|
|
207
|
+
ceiling = ceiling_limits()[key]
|
|
208
|
+
if key not in definition or definition[key] is None:
|
|
209
|
+
return ceiling
|
|
210
|
+
value = _require_int(definition[key], key, min_value=1)
|
|
211
|
+
return min(value, ceiling)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def require_object(value: Any, name: str) -> dict:
|
|
215
|
+
if value is None:
|
|
216
|
+
return {}
|
|
217
|
+
if not isinstance(value, dict):
|
|
218
|
+
raise HandlerError(f"{name} must be an object")
|
|
219
|
+
return value
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def require_str(data: dict, key: str) -> str:
|
|
223
|
+
value = data.get(key)
|
|
224
|
+
if not isinstance(value, str) or not value.strip():
|
|
225
|
+
raise HandlerError(f"{key} is required")
|
|
226
|
+
return value.strip()
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def optional_str(data: dict, key: str) -> str | None:
|
|
230
|
+
if key not in data or data[key] is None:
|
|
231
|
+
return None
|
|
232
|
+
value = data[key]
|
|
233
|
+
if not isinstance(value, str):
|
|
234
|
+
raise HandlerError(f"{key} must be a string")
|
|
235
|
+
stripped = value.strip()
|
|
236
|
+
return stripped if stripped else None
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def optional_bool(data: dict, key: str) -> bool | None:
|
|
240
|
+
if key not in data or data[key] is None:
|
|
241
|
+
return None
|
|
242
|
+
value = data[key]
|
|
243
|
+
if not isinstance(value, bool):
|
|
244
|
+
raise HandlerError(f"{key} must be a boolean")
|
|
245
|
+
return value
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def optional_int(
|
|
249
|
+
data: dict,
|
|
250
|
+
key: str,
|
|
251
|
+
*,
|
|
252
|
+
min_value: int | None = None,
|
|
253
|
+
max_value: int | None = None,
|
|
254
|
+
) -> int | None:
|
|
255
|
+
if key not in data or data[key] is None:
|
|
256
|
+
return None
|
|
257
|
+
return _require_int(data[key], key, min_value=min_value, max_value=max_value)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _require_int(
|
|
261
|
+
value: Any,
|
|
262
|
+
key: str,
|
|
263
|
+
*,
|
|
264
|
+
min_value: int | None = None,
|
|
265
|
+
max_value: int | None = None,
|
|
266
|
+
) -> int:
|
|
267
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
268
|
+
raise HandlerError(f"{key} must be an integer")
|
|
269
|
+
if min_value is not None and value < min_value:
|
|
270
|
+
raise HandlerError(f"{key} must be at least {min_value}")
|
|
271
|
+
if max_value is not None and value > max_value:
|
|
272
|
+
raise HandlerError(f"{key} must be at most {max_value}")
|
|
273
|
+
return value
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def optional_str_dict(data: dict, key: str) -> dict[str, str] | None:
|
|
277
|
+
if key not in data or data[key] is None:
|
|
278
|
+
return None
|
|
279
|
+
value = data[key]
|
|
280
|
+
if not isinstance(value, dict):
|
|
281
|
+
raise HandlerError(f"{key} must be an object")
|
|
282
|
+
result: dict[str, str] = {}
|
|
283
|
+
for item_key, item_value in value.items():
|
|
284
|
+
if not isinstance(item_key, str) or not item_key:
|
|
285
|
+
raise HandlerError(f"{key} keys must be non-empty strings")
|
|
286
|
+
if not isinstance(item_value, str):
|
|
287
|
+
raise HandlerError(f"{key}.{item_key} must be a string")
|
|
288
|
+
result[item_key] = item_value
|
|
289
|
+
return result
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def require_env_vars(data: dict, key: str) -> dict[str, str] | None:
|
|
293
|
+
env_vars = optional_str_dict(data, key)
|
|
294
|
+
if env_vars is None:
|
|
295
|
+
return None
|
|
296
|
+
for name in env_vars:
|
|
297
|
+
if not ENV_KEY_RE.match(name):
|
|
298
|
+
raise HandlerError(f"{key}.{name} is not a valid environment variable name")
|
|
299
|
+
return env_vars
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def resolve_cwd(data: dict) -> str | None:
|
|
303
|
+
cwd = optional_str(data, "cwd")
|
|
304
|
+
pwd = optional_str(data, "pwd")
|
|
305
|
+
if cwd and pwd and cwd != pwd:
|
|
306
|
+
raise HandlerError("cwd and pwd must match when both are provided")
|
|
307
|
+
return cwd or pwd
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def resolve_timeout(
|
|
311
|
+
input_data: dict,
|
|
312
|
+
definition: dict,
|
|
313
|
+
*,
|
|
314
|
+
ceiling_key: str,
|
|
315
|
+
default: int | None = None,
|
|
316
|
+
) -> int:
|
|
317
|
+
ceiling = effective_limit(definition, ceiling_key)
|
|
318
|
+
value = default if default is not None else ceiling
|
|
319
|
+
value = min(value, ceiling)
|
|
320
|
+
definition_timeout = optional_int(definition, "timeout", min_value=1)
|
|
321
|
+
input_timeout = optional_int(input_data, "timeout", min_value=1)
|
|
322
|
+
if definition_timeout is not None:
|
|
323
|
+
value = min(value, definition_timeout)
|
|
324
|
+
if input_timeout is not None:
|
|
325
|
+
value = min(value, input_timeout)
|
|
326
|
+
return value
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def enum_value(value: Any) -> Any:
|
|
330
|
+
if value is None:
|
|
331
|
+
return None
|
|
332
|
+
if isinstance(value, Enum):
|
|
333
|
+
return value.value
|
|
334
|
+
if hasattr(value, "value") and not callable(value.value):
|
|
335
|
+
inner = value.value
|
|
336
|
+
if isinstance(inner, Enum):
|
|
337
|
+
return inner.value
|
|
338
|
+
if isinstance(inner, (str, int, float, bool)):
|
|
339
|
+
return inner
|
|
340
|
+
if isinstance(value, (str, int, float, bool, dict, list)):
|
|
341
|
+
return value
|
|
342
|
+
return str(value)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def serialize_sandbox(sandbox: Sandbox) -> dict[str, Any]:
|
|
346
|
+
data: dict[str, Any] = {}
|
|
347
|
+
for field in SANDBOX_PUBLIC_FIELDS:
|
|
348
|
+
data[field] = enum_value(getattr(sandbox, field, None))
|
|
349
|
+
return data
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def serialize_file_info(info: Any) -> dict[str, Any]:
|
|
353
|
+
return {
|
|
354
|
+
"name": getattr(info, "name", None),
|
|
355
|
+
"path": getattr(info, "path", None),
|
|
356
|
+
"is_dir": getattr(info, "is_dir", None),
|
|
357
|
+
"size": getattr(info, "size", None),
|
|
358
|
+
"mode": getattr(info, "mode", None),
|
|
359
|
+
"permissions": getattr(info, "permissions", None),
|
|
360
|
+
"owner": getattr(info, "owner", None),
|
|
361
|
+
"group": getattr(info, "group", None),
|
|
362
|
+
"mod_time": getattr(info, "mod_time", None),
|
|
363
|
+
"modified_at": getattr(info, "modified_at", None),
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def truncate_text(text: str | None, *, max_bytes: int, max_lines: int) -> dict[str, Any]:
|
|
368
|
+
raw = text or ""
|
|
369
|
+
lines = raw.splitlines()
|
|
370
|
+
total_lines = len(lines)
|
|
371
|
+
truncated_lines = total_lines > max_lines
|
|
372
|
+
if truncated_lines:
|
|
373
|
+
lines = lines[:max_lines]
|
|
374
|
+
clipped = "\n".join(lines)
|
|
375
|
+
encoded = clipped.encode("utf-8")
|
|
376
|
+
truncated_bytes = len(encoded) > max_bytes
|
|
377
|
+
if truncated_bytes:
|
|
378
|
+
encoded = encoded[:max_bytes]
|
|
379
|
+
while encoded and (encoded[-1] & 0xC0) == 0x80:
|
|
380
|
+
encoded = encoded[:-1]
|
|
381
|
+
clipped = encoded.decode("utf-8", errors="ignore")
|
|
382
|
+
return {
|
|
383
|
+
"text": clipped,
|
|
384
|
+
"truncated": truncated_lines or truncated_bytes,
|
|
385
|
+
"truncated_lines": truncated_lines,
|
|
386
|
+
"truncated_bytes": truncated_bytes,
|
|
387
|
+
"returned_lines": 0 if clipped == "" and total_lines == 0 else len(clipped.splitlines()) if clipped else 0,
|
|
388
|
+
"total_lines": total_lines,
|
|
389
|
+
"byte_count": len(clipped.encode("utf-8")),
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def slice_text_lines(
|
|
394
|
+
text: str,
|
|
395
|
+
*,
|
|
396
|
+
start_line: int,
|
|
397
|
+
line_limit: int,
|
|
398
|
+
max_bytes: int,
|
|
399
|
+
) -> dict[str, Any]:
|
|
400
|
+
lines = text.splitlines()
|
|
401
|
+
total_lines = len(lines)
|
|
402
|
+
start_index = start_line - 1
|
|
403
|
+
selected = lines[start_index:]
|
|
404
|
+
truncated_lines = len(selected) > line_limit
|
|
405
|
+
if truncated_lines:
|
|
406
|
+
selected = selected[:line_limit]
|
|
407
|
+
clipped = "\n".join(selected)
|
|
408
|
+
encoded = clipped.encode("utf-8")
|
|
409
|
+
truncated_bytes = len(encoded) > max_bytes
|
|
410
|
+
if truncated_bytes:
|
|
411
|
+
encoded = encoded[:max_bytes]
|
|
412
|
+
while encoded and (encoded[-1] & 0xC0) == 0x80:
|
|
413
|
+
encoded = encoded[:-1]
|
|
414
|
+
clipped = encoded.decode("utf-8", errors="ignore")
|
|
415
|
+
return {
|
|
416
|
+
"text": clipped,
|
|
417
|
+
"start_line": start_line,
|
|
418
|
+
"truncated": truncated_lines or truncated_bytes,
|
|
419
|
+
"truncated_lines": truncated_lines,
|
|
420
|
+
"truncated_bytes": truncated_bytes,
|
|
421
|
+
"returned_lines": 0 if not clipped else len(clipped.splitlines()),
|
|
422
|
+
"total_lines": total_lines,
|
|
423
|
+
"byte_count": len(clipped.encode("utf-8")),
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def require_path(data: dict, key: str = "path") -> str:
|
|
428
|
+
path = require_str(data, key)
|
|
429
|
+
if "\x00" in path:
|
|
430
|
+
raise HandlerError(f"{key} contains NUL")
|
|
431
|
+
return path
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def get_sandbox(daytona: Daytona, sandbox_id: str) -> Sandbox:
|
|
435
|
+
sandbox = call_daytona(
|
|
436
|
+
lambda: daytona.get(sandbox_id, request_timeout=SANDBOX_LOOKUP_TIMEOUT_SECONDS),
|
|
437
|
+
retry=True,
|
|
438
|
+
)
|
|
439
|
+
if sandbox is None:
|
|
440
|
+
raise HandlerError("Sandbox not found", "not_found")
|
|
441
|
+
return sandbox
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def sandbox_state(sandbox: Sandbox) -> str | None:
|
|
445
|
+
return enum_value(getattr(sandbox, "state", None))
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
def request_timeout_for_sandbox(sandbox: Sandbox) -> int:
|
|
449
|
+
"""Seconds to wait for a Daytona call that should return promptly.
|
|
450
|
+
|
|
451
|
+
Cold sandboxes take longer to answer, but every value is finite so a
|
|
452
|
+
dropped connection cannot pin a worker thread.
|
|
453
|
+
"""
|
|
454
|
+
state = sandbox_state(sandbox)
|
|
455
|
+
if state in _RUNNING_STATES:
|
|
456
|
+
return REQUEST_TIMEOUT_RUNNING_SECONDS
|
|
457
|
+
if state == "archived":
|
|
458
|
+
return REQUEST_TIMEOUT_ARCHIVED_SECONDS
|
|
459
|
+
return REQUEST_TIMEOUT_STOPPED_SECONDS
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def call_daytona(action: Callable[[], T], *, retry: bool) -> T:
|
|
463
|
+
"""Run a Daytona SDK call with a bounded number of transport retries.
|
|
464
|
+
|
|
465
|
+
Writes must pass ``retry=False``. Timeouts and connection drops are
|
|
466
|
+
retried only when ``retry`` is true. A command that exceeded its own
|
|
467
|
+
execution timeout is not retried and keeps the SDK error message.
|
|
468
|
+
"""
|
|
469
|
+
attempts = DAYTONA_RETRY_ATTEMPTS if retry else 1
|
|
470
|
+
last_transport: DaytonaError | None = None
|
|
471
|
+
for attempt in range(attempts):
|
|
472
|
+
try:
|
|
473
|
+
return action()
|
|
474
|
+
except DaytonaProcessExecutionTimeoutError as exc:
|
|
475
|
+
raise map_daytona(exc) from exc
|
|
476
|
+
except DaytonaError as exc:
|
|
477
|
+
if not _retryable_transport_error(exc):
|
|
478
|
+
raise map_daytona(exc) from exc
|
|
479
|
+
last_transport = exc
|
|
480
|
+
if attempt + 1 >= attempts:
|
|
481
|
+
break
|
|
482
|
+
time.sleep(DAYTONA_RETRY_BASE_SECONDS * (2**attempt))
|
|
483
|
+
assert last_transport is not None
|
|
484
|
+
if isinstance(last_transport, DaytonaTimeoutError):
|
|
485
|
+
raise HandlerError(DAYTONA_TIMEOUT_MESSAGE, "timeout") from last_transport
|
|
486
|
+
raise map_daytona(last_transport) from last_transport
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def _retryable_transport_error(exc: DaytonaError) -> bool:
|
|
490
|
+
if isinstance(exc, DaytonaProcessExecutionTimeoutError):
|
|
491
|
+
return False
|
|
492
|
+
return isinstance(exc, (DaytonaTimeoutError, DaytonaConnectionError))
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def ensure_started(sandbox: Sandbox, timeout: int) -> Sandbox:
|
|
496
|
+
state = sandbox_state(sandbox)
|
|
497
|
+
if state == "started":
|
|
498
|
+
return sandbox
|
|
499
|
+
if state in UNSTARTABLE_STATES:
|
|
500
|
+
raise HandlerError(
|
|
501
|
+
f"Sandbox cannot be started from state {state}",
|
|
502
|
+
"conflict",
|
|
503
|
+
)
|
|
504
|
+
sandbox.start(timeout=timeout)
|
|
505
|
+
refresh_sandbox(sandbox)
|
|
506
|
+
return sandbox
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def refresh_sandbox(sandbox: Sandbox) -> None:
|
|
510
|
+
refresh = getattr(sandbox, "refresh_data", None)
|
|
511
|
+
if callable(refresh):
|
|
512
|
+
call_daytona(
|
|
513
|
+
lambda: refresh(request_timeout=SANDBOX_LOOKUP_TIMEOUT_SECONDS),
|
|
514
|
+
retry=True,
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def require_sandbox_id(input_data: dict) -> str:
|
|
519
|
+
return require_str(input_data, "sandbox_id")
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def get_running_sandbox(
|
|
523
|
+
daytona: Daytona,
|
|
524
|
+
input_data: dict,
|
|
525
|
+
definition: dict,
|
|
526
|
+
) -> Sandbox:
|
|
527
|
+
sandbox = get_sandbox(daytona, require_sandbox_id(input_data))
|
|
528
|
+
timeout = resolve_timeout(
|
|
529
|
+
input_data,
|
|
530
|
+
definition,
|
|
531
|
+
ceiling_key="max_lifecycle_timeout",
|
|
532
|
+
default=60,
|
|
533
|
+
)
|
|
534
|
+
return ensure_started(sandbox, timeout)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
from daytona import (
|
|
2
|
+
CreateSandboxFromImageParams,
|
|
3
|
+
CreateSandboxFromSnapshotParams,
|
|
4
|
+
Daytona,
|
|
5
|
+
DaytonaError,
|
|
6
|
+
GpuType,
|
|
7
|
+
Resources,
|
|
8
|
+
VolumeMount,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from continuum_task_server_bundle.daytona.common import (
|
|
12
|
+
HandlerError,
|
|
13
|
+
effective_limit,
|
|
14
|
+
map_daytona,
|
|
15
|
+
optional_bool,
|
|
16
|
+
optional_int,
|
|
17
|
+
optional_str,
|
|
18
|
+
optional_str_dict,
|
|
19
|
+
require_env_vars,
|
|
20
|
+
require_object,
|
|
21
|
+
resolve_timeout,
|
|
22
|
+
serialize_sandbox,
|
|
23
|
+
success,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ALLOWED_LANGUAGES = {"python", "typescript", "javascript"}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def create_sandbox(daytona: Daytona, definition: dict, input_data: dict) -> dict:
|
|
30
|
+
_ = require_object(input_data, "input_data_json")
|
|
31
|
+
image = optional_str(definition, "image")
|
|
32
|
+
snapshot = optional_str(definition, "snapshot")
|
|
33
|
+
if image and snapshot:
|
|
34
|
+
raise HandlerError("Provide either image or snapshot, not both")
|
|
35
|
+
|
|
36
|
+
base_kwargs = _create_base_kwargs(definition)
|
|
37
|
+
resources = _parse_resources(definition.get("resources"))
|
|
38
|
+
timeout = resolve_timeout(
|
|
39
|
+
input_data,
|
|
40
|
+
definition,
|
|
41
|
+
ceiling_key="max_create_timeout",
|
|
42
|
+
default=min(60, effective_limit(definition, "max_create_timeout")),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
if image:
|
|
46
|
+
params = CreateSandboxFromImageParams(
|
|
47
|
+
image=image,
|
|
48
|
+
resources=resources,
|
|
49
|
+
**base_kwargs,
|
|
50
|
+
)
|
|
51
|
+
else:
|
|
52
|
+
if resources is not None:
|
|
53
|
+
raise HandlerError("resources can only be set when creating from an image")
|
|
54
|
+
params = CreateSandboxFromSnapshotParams(
|
|
55
|
+
snapshot=snapshot,
|
|
56
|
+
**base_kwargs,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
sandbox = daytona.create(params, timeout=timeout)
|
|
61
|
+
except DaytonaError as exc:
|
|
62
|
+
raise map_daytona(exc) from exc
|
|
63
|
+
|
|
64
|
+
return success(serialize_sandbox(sandbox))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _create_base_kwargs(definition: dict) -> dict:
|
|
68
|
+
language = optional_str(definition, "language")
|
|
69
|
+
if language and language not in ALLOWED_LANGUAGES:
|
|
70
|
+
raise HandlerError("language must be python, typescript, or javascript")
|
|
71
|
+
|
|
72
|
+
kwargs: dict = {}
|
|
73
|
+
mapping = {
|
|
74
|
+
"name": optional_str(definition, "name"),
|
|
75
|
+
"language": language,
|
|
76
|
+
"os_user": optional_str(definition, "os_user"),
|
|
77
|
+
"env_vars": require_env_vars(definition, "env_vars"),
|
|
78
|
+
"labels": optional_str_dict(definition, "labels"),
|
|
79
|
+
"public": optional_bool(definition, "public"),
|
|
80
|
+
"auto_stop_interval": optional_int(definition, "auto_stop_interval", min_value=0),
|
|
81
|
+
"auto_pause_interval": optional_int(definition, "auto_pause_interval", min_value=0),
|
|
82
|
+
"auto_archive_interval": optional_int(definition, "auto_archive_interval", min_value=0),
|
|
83
|
+
"auto_delete_interval": optional_int(definition, "auto_delete_interval", min_value=-1),
|
|
84
|
+
"ttl_minutes": optional_int(definition, "ttl_minutes", min_value=0),
|
|
85
|
+
"volumes": _parse_volumes(definition.get("volumes")),
|
|
86
|
+
"secrets": optional_str_dict(definition, "secrets"),
|
|
87
|
+
"network_block_all": optional_bool(definition, "network_block_all"),
|
|
88
|
+
"network_allow_list": optional_str(definition, "network_allow_list"),
|
|
89
|
+
"domain_allow_list": optional_str(definition, "domain_allow_list"),
|
|
90
|
+
"outbound_proxy_url": optional_str(definition, "outbound_proxy_url"),
|
|
91
|
+
"ephemeral": optional_bool(definition, "ephemeral"),
|
|
92
|
+
"spot": optional_bool(definition, "spot"),
|
|
93
|
+
"linked_sandbox": optional_str(definition, "linked_sandbox"),
|
|
94
|
+
}
|
|
95
|
+
for key, value in mapping.items():
|
|
96
|
+
if value is not None:
|
|
97
|
+
kwargs[key] = value
|
|
98
|
+
return kwargs
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _parse_resources(value) -> Resources | None:
|
|
102
|
+
if value is None:
|
|
103
|
+
return None
|
|
104
|
+
if not isinstance(value, dict):
|
|
105
|
+
raise HandlerError("resources must be an object")
|
|
106
|
+
gpu_type = value.get("gpu_type")
|
|
107
|
+
parsed_gpu_type = _parse_gpu_type(gpu_type) if gpu_type is not None else None
|
|
108
|
+
kwargs = {
|
|
109
|
+
"cpu": optional_int(value, "cpu", min_value=1),
|
|
110
|
+
"memory": optional_int(value, "memory", min_value=1),
|
|
111
|
+
"disk": optional_int(value, "disk", min_value=1),
|
|
112
|
+
"gpu": optional_int(value, "gpu", min_value=1),
|
|
113
|
+
"gpu_type": parsed_gpu_type,
|
|
114
|
+
}
|
|
115
|
+
cleaned = {key: item for key, item in kwargs.items() if item is not None}
|
|
116
|
+
return Resources(**cleaned) if cleaned else Resources()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _parse_gpu_type(value):
|
|
120
|
+
if isinstance(value, list):
|
|
121
|
+
if not value:
|
|
122
|
+
raise HandlerError("gpu_type must not be empty")
|
|
123
|
+
return [_parse_one_gpu(item) for item in value]
|
|
124
|
+
return _parse_one_gpu(value)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _parse_one_gpu(value) -> GpuType:
|
|
128
|
+
if not isinstance(value, str) or not value.strip():
|
|
129
|
+
raise HandlerError("gpu_type values must be non-empty strings")
|
|
130
|
+
token = value.strip()
|
|
131
|
+
try:
|
|
132
|
+
return GpuType(token)
|
|
133
|
+
except ValueError:
|
|
134
|
+
normalized = token.replace("-", "_").replace(" ", "_").upper()
|
|
135
|
+
try:
|
|
136
|
+
return GpuType[normalized]
|
|
137
|
+
except KeyError as exc:
|
|
138
|
+
raise HandlerError(f"Unknown gpu_type: {token}") from exc
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _parse_volumes(value) -> list[VolumeMount] | None:
|
|
142
|
+
if value is None:
|
|
143
|
+
return None
|
|
144
|
+
if not isinstance(value, list):
|
|
145
|
+
raise HandlerError("volumes must be a list")
|
|
146
|
+
mounts: list[VolumeMount] = []
|
|
147
|
+
for index, item in enumerate(value):
|
|
148
|
+
if not isinstance(item, dict):
|
|
149
|
+
raise HandlerError(f"volumes[{index}] must be an object")
|
|
150
|
+
volume_id = item.get("volume_id")
|
|
151
|
+
mount_path = item.get("mount_path")
|
|
152
|
+
if not isinstance(volume_id, str) or not volume_id.strip():
|
|
153
|
+
raise HandlerError(f"volumes[{index}].volume_id is required")
|
|
154
|
+
if not isinstance(mount_path, str) or not mount_path.strip():
|
|
155
|
+
raise HandlerError(f"volumes[{index}].mount_path is required")
|
|
156
|
+
kwargs = {
|
|
157
|
+
"volume_id": volume_id.strip(),
|
|
158
|
+
"mount_path": mount_path.strip(),
|
|
159
|
+
}
|
|
160
|
+
subpath = item.get("subpath")
|
|
161
|
+
if subpath is not None:
|
|
162
|
+
if not isinstance(subpath, str) or not subpath.strip():
|
|
163
|
+
raise HandlerError(f"volumes[{index}].subpath must be a string")
|
|
164
|
+
kwargs["subpath"] = subpath.strip()
|
|
165
|
+
mounts.append(VolumeMount(**kwargs))
|
|
166
|
+
return mounts
|