solari-runloop-compat 0.2.10__tar.gz → 0.2.12__tar.gz

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.
Files changed (55) hide show
  1. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/COMPAT.md +6 -0
  2. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/PKG-INFO +1 -1
  3. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/pyproject.toml +1 -1
  4. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/__init__.py +1 -1
  5. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_context.py +5 -2
  6. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_gateway.py +53 -4
  7. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_mapping.py +18 -0
  8. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/.gitignore +0 -0
  9. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/LICENSE +0 -0
  10. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/NOTICE +0 -0
  11. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/README.md +0 -0
  12. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_branding.py +0 -0
  13. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_client.py +0 -0
  14. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_config.py +0 -0
  15. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_dockerfile.py +0 -0
  16. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_exceptions.py +0 -0
  17. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_loop.py +0 -0
  18. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_models.py +0 -0
  19. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_options.py +0 -0
  20. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_redact.py +0 -0
  21. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_resources.py +0 -0
  22. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_sync.py +0 -0
  23. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_types.py +0 -0
  24. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/_vendor_user.py +0 -0
  25. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/lib/__init__.py +0 -0
  26. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/lib/polling.py +0 -0
  27. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/pagination.py +0 -0
  28. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/sdk.py +0 -0
  29. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/__init__.py +0 -0
  30. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/blueprint_build_log.py +0 -0
  31. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/blueprint_build_logs_list_view.py +0 -0
  32. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/blueprint_build_parameters.py +0 -0
  33. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/blueprint_list_view.py +0 -0
  34. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/blueprint_view.py +0 -0
  35. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devbox_async_execution_detail_view.py +0 -0
  36. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devbox_execution_detail_view.py +0 -0
  37. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devbox_snapshot_list_view.py +0 -0
  38. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devbox_snapshot_view.py +0 -0
  39. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devbox_view.py +0 -0
  40. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devboxes/__init__.py +0 -0
  41. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devboxes/devbox_logs_list_view.py +0 -0
  42. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devboxes/devbox_snapshot_async_status_view.py +0 -0
  43. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/devboxes/execution_update_chunk.py +0 -0
  44. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/object_download_url_view.py +0 -0
  45. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/object_view.py +0 -0
  46. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/secret_list_view.py +0 -0
  47. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/secret_view.py +0 -0
  48. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/__init__.py +0 -0
  49. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/after_idle.py +0 -0
  50. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/code_mount_parameters.py +0 -0
  51. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/launch_parameters.py +0 -0
  52. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/lifecycle_configuration.py +0 -0
  53. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/lifecycle_hooks.py +0 -0
  54. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/shared/resume_triggers.py +0 -0
  55. {solari_runloop_compat-0.2.10 → solari_runloop_compat-0.2.12}/solari_runloop/types/tunnel_view.py +0 -0
@@ -127,6 +127,12 @@ import) keep working but still lack the image's `ENV`: rebuild them.
127
127
  `DevboxView` fields Solari does not have are synthesized: `create_time_ms` from `createdAt`, one `state_transitions`
128
128
  entry, `launch_parameters` from the real `cpu`/`memMb`, `capabilities=[]`.
129
129
  - Solari's `stopped` state (guest powered itself off; `POST /sandboxes/:id/start`) has no Runloop equivalent: there is no `Devbox.start`, and a `stopped` sandbox reports status `shutdown` (same as the TypeScript package). Use `solari_sandbox`'s `start()` directly.
130
+ - A suspend ends the command a client is following: the process is saved with the devbox and runs on after a resume, but the execution
131
+ the gateway kept (or the control channel it was read through) is closed. `result()` then raises an `APIError` that says so,
132
+ `exit_status` stays `None`, and `get_state()` says `running` (the process is alive; it is not `completed`) (it used to return at once with `exit_status=None`, which read as success).
133
+ - A control channel the gateway refuses with HTTP 404 (a devbox deleted under a cached handle) is a `NotFoundError`, not an `APIConnectionError`.
134
+ `net.get_tunnel_url(port)` of an authenticated tunnel carries `?slr_token=` (as the TypeScript compat does); an open tunnel's URL stays bare.
135
+ An `async def` log callback is awaited; one that raises is logged and never breaks the execution.
130
136
  - Errors: 400 `BadRequestError`, 401 `AuthenticationError`, 402 and 403 `PermissionDeniedError` (`status_code` keeps 402),
131
137
  404 `NotFoundError`, 409 `ConflictError`, 422 `UnprocessableEntityError`, 429 `RateLimitError`, 5xx `InternalServerError`;
132
138
  network -> `APIConnectionError`, timeouts -> `APITimeoutError`. `.body` is Solari's `{code,error,message,retryable}`.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: solari-runloop-compat
3
- Version: 0.2.10
3
+ Version: 0.2.12
4
4
  Summary: Runloop-compatible Python interface over Solari Sandboxes. Not affiliated with Runloop.
5
5
  Project-URL: Homepage, https://getsolari.com
6
6
  Author: Solari
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "solari-runloop-compat"
7
- version = "0.2.10"
7
+ version = "0.2.12"
8
8
  description = "Runloop-compatible Python interface over Solari Sandboxes. Not affiliated with Runloop."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -33,7 +33,7 @@ from ._types import NOT_GIVEN, NoneType, NotGiven, Omit, Timeout, not_given, omi
33
33
  from .lib.polling import PollingConfig, PollingTimeout
34
34
  from .sdk import AsyncRunloopSDK, RunloopSDK
35
35
 
36
- __version__ = "0.2.10"
36
+ __version__ = "0.2.12"
37
37
  Client = Runloop
38
38
  AsyncClient = AsyncRunloop
39
39
 
@@ -8,6 +8,7 @@ the context is capped at ~700 KB). When objects land, :class:`LocalContext` is r
8
8
  from __future__ import annotations
9
9
 
10
10
  import fnmatch
11
+ import gzip
11
12
  import hashlib
12
13
  import io
13
14
  import os
@@ -40,7 +41,9 @@ def _ignored(rel: str, patterns: List[str]) -> bool:
40
41
 
41
42
  def _make(entries: List[tuple], name: Optional[str]) -> LocalContext:
42
43
  buf = io.BytesIO()
43
- with tarfile.open(fileobj=buf, mode="w:gz", compresslevel=9) as tf:
44
+ # GzipFile with mtime=0: tarfile's own "w:gz" stamps the gzip header with the wall clock, so two builds of one tree a second
45
+ # apart had different bytes and a different ctx_ id.
46
+ with gzip.GzipFile(fileobj=buf, mode="wb", compresslevel=9, mtime=0) as gz, tarfile.open(fileobj=gz, mode="w") as tf:
44
47
  for arc, path in entries:
45
48
  info = tf.gettarinfo(str(path), arcname=arc)
46
49
  info.mtime, info.uid, info.gid, info.uname, info.gname = 0, 0, 0, "", ""
@@ -81,7 +84,7 @@ def from_dir(path: str, *, name: Optional[str] = None, ignore: Optional[List[str
81
84
 
82
85
  def from_bytes(data: bytes, name: str = "file") -> LocalContext:
83
86
  buf = io.BytesIO()
84
- with tarfile.open(fileobj=buf, mode="w:gz") as tf:
87
+ with gzip.GzipFile(fileobj=buf, mode="wb", mtime=0) as gz, tarfile.open(fileobj=gz, mode="w") as tf:
85
88
  info = tarfile.TarInfo(name)
86
89
  info.size, info.mode = len(data), 0o644
87
90
  tf.addfile(info, io.BytesIO(data))
@@ -9,6 +9,7 @@ suspend/resume/shutdown so the next call reconnects against the live session.
9
9
  from __future__ import annotations
10
10
 
11
11
  import asyncio
12
+ import inspect
12
13
  import logging
13
14
  import posixpath
14
15
  import shlex
@@ -54,6 +55,16 @@ def _q(s: str) -> str:
54
55
  return quote(s, safe="")
55
56
 
56
57
 
58
+ _CALLBACK_TASKS: "set[asyncio.Task[None]]" = set() # keeps async log callbacks alive until they finish
59
+
60
+
61
+ async def _await_callback(aw: Any) -> None:
62
+ try:
63
+ await aw
64
+ except Exception: # noqa: BLE001 - a user callback must not break the exec
65
+ log.exception("async log callback raised")
66
+
67
+
57
68
  @dataclass
58
69
  class _Exec:
59
70
  execution_id: str
@@ -70,6 +81,7 @@ class _Exec:
70
81
  started_ms: int = field(default_factory=lambda: int(time.time() * 1000))
71
82
  shell_name: Optional[str] = None
72
83
  error: Optional[BaseException] = None
84
+ lost: bool = False # the control channel dropped (a suspend): the process may well be alive, so the state is not "completed"
73
85
  remote: bool = False # started through the gateway's durable executions
74
86
  truncated: Tuple[bool, bool] = (False, False) # (stdout, stderr), as reported by the gateway
75
87
 
@@ -98,7 +110,16 @@ class _Exec:
98
110
  if cb is None:
99
111
  return
100
112
  try:
101
- cb(line)
113
+ res = cb(line)
114
+ if inspect.isawaitable(res): # `async def` callback: run it on the loop (it used to be dropped as "never awaited")
115
+ try:
116
+ task = asyncio.get_running_loop().create_task(_await_callback(res))
117
+ except RuntimeError: # no running loop on this thread
118
+ getattr(res, "close", lambda: None)()
119
+ log.warning("async log callback dropped: no running event loop")
120
+ else:
121
+ _CALLBACK_TASKS.add(task)
122
+ task.add_done_callback(_CALLBACK_TASKS.discard)
102
123
  except Exception: # noqa: BLE001 - a user callback must not break the exec
103
124
  log.exception("log callback raised")
104
125
 
@@ -106,7 +127,7 @@ class _Exec:
106
127
  return DevboxAsyncExecutionDetailView(
107
128
  devbox_id=self.devbox_id,
108
129
  execution_id=self.execution_id,
109
- status=self.status, # type: ignore[arg-type]
130
+ status="running" if self.lost else self.status, # type: ignore[arg-type]
110
131
  exit_status=self.exit_code,
111
132
  stdout=mp.last_n_lines("".join(self.stdout), last_n),
112
133
  stderr=mp.last_n_lines("".join(self.stderr), last_n),
@@ -137,6 +158,7 @@ class Gateway:
137
158
  self._tunnel_api: Optional[bool] = None # gateway has /sandboxes/{id}/tunnel
138
159
  self._deleted_snapshots: set = set() # snapshot ids this client deleted, so query_status can say 'deleted'
139
160
  self._snapshot_sources: Dict[str, str] = {} # snapshot id -> devbox it was made from (the gateway's list does not say yet)
161
+ self._interrupted: set = set() # execution ids the gateway closed because the devbox was suspended (the process lives on)
140
162
  self._durable: Optional[bool] = None # gateway has /sandboxes/{id}/executions
141
163
  self._remote: Dict[str, str] = {} # execution id -> devbox id, for executions started through the durable API
142
164
  self.contexts: Dict[str, Any] = {} # build contexts by object id (in-process; see _context.py)
@@ -503,7 +525,8 @@ class Gateway:
503
525
  return DevboxAsyncExecutionDetailView(
504
526
  devbox_id=devbox_id,
505
527
  execution_id=d["executionId"],
506
- status=d.get("status", "running"),
528
+ # The gateway closed the record because a suspend cut the channel; the process is saved and runs on after the resume.
529
+ status="running" if d.get("interrupted") == "pause" and d.get("exitCode") is None else d.get("status", "running"),
507
530
  exit_status=d.get("exitCode"),
508
531
  stdout=mp.last_n_lines(d.get("stdout", ""), last_n),
509
532
  stderr=mp.last_n_lines(d.get("stderr", ""), last_n),
@@ -553,6 +576,16 @@ class Gateway:
553
576
  rec.exit_code = final.exit_status
554
577
  rec.stdout, rec.stderr = [final.stdout or ""], [final.stderr or ""]
555
578
  rec.truncated = (bool(final.stdout_truncated), bool(final.stderr_truncated))
579
+ if rec.execution_id in self._interrupted:
580
+ rec.lost = True
581
+ # "completed" with no exit status read as success. The process is saved with the devbox and runs on after a resume;
582
+ # this client just cannot follow it any more.
583
+ rec.error = ex.APIError(
584
+ f"execution {rec.execution_id} was interrupted by a suspend of the devbox: the process is saved with it and keeps running after a resume, "
585
+ "but its output and exit status can no longer be followed. Start the command again (or have it write a log file) to observe it.",
586
+ None,
587
+ body=None,
588
+ )
556
589
  break
557
590
  except Exception as e: # noqa: BLE001
558
591
  # A wait that cannot be answered right now (the devbox is suspended: 409; connection or 5xx while it resumes) says
@@ -579,6 +612,8 @@ class Gateway:
579
612
  raise PollingTimeout("Timed out waiting for execution", await self.remote_get(devbox_id, eid))
580
613
  d = await self._exec_call("POST", f"/sandboxes/{_q(devbox_id)}/executions/{_q(eid)}/wait", {"timeoutMs": int(min(25.0, left if left is not None else 25.0) * 1000)})
581
614
  if d.get("status") == "completed":
615
+ if d.get("interrupted") == "pause" and d.get("exitCode") is None:
616
+ self._interrupted.add(eid)
582
617
  return self._remote_view(d, devbox_id)
583
618
 
584
619
  async def _fresh_request(self, method: str, path: str, body: Optional[Dict[str, Any]] = None) -> Any:
@@ -657,6 +692,15 @@ class Gateway:
657
692
  except Exception as e: # noqa: BLE001 - channel dropped before cmd.exit
658
693
  log.warning("execution %s lost its control channel: %s", rec.execution_id, _redact(str(e), rec.devbox_id))
659
694
  rec.exit_code = None
695
+ rec.lost = True
696
+ # No exit status because the channel dropped (a suspend, an expiry), not because the program ended: say so instead of
697
+ # letting result() return a "completed" run with exit_status None.
698
+ rec.error = ex.APIError(
699
+ f"execution {rec.execution_id} lost its control channel before the program reported an exit status (a suspend or the end of the devbox); "
700
+ "if the devbox was suspended the process keeps running after a resume, but this client can no longer follow it.",
701
+ None,
702
+ body=None,
703
+ )
660
704
  rec.flush()
661
705
  rec.status = "completed"
662
706
  assert rec.done is not None
@@ -1344,7 +1388,12 @@ class Gateway:
1344
1388
  return None
1345
1389
  template = getattr(t, "url_template", None)
1346
1390
  if template:
1347
- return template.replace("{port}", str(port))
1391
+ url = template.replace("{port}", str(port))
1392
+ # An authenticated tunnel answers 401 to a bare URL; the TypeScript compat returns the URL with the tunnel's token, so does this.
1393
+ token = getattr(t, "auth_token", None)
1394
+ if token and getattr(t, "auth_mode", None) == "authenticated" and "slr_token=" not in url:
1395
+ url += ("&" if "?" in url else "?") + "slr_token=" + quote(token, safe="")
1396
+ return url
1348
1397
  return (await self.preview_url(devbox_id, port)).get("url")
1349
1398
 
1350
1399
  async def tunnel_view(self, devbox_id: str, port: int = 8080) -> Any:
@@ -403,6 +403,15 @@ def create_body(
403
403
  # --- errors ---------------------------------------------------------------------------------
404
404
 
405
405
 
406
+ #: A file path the guest's image-root guard refused (a devbox built from an image): it resolves outside the image root,
407
+ #: through a magic link such as /proc/1/root, or across a mount. Runloop answers that with PermissionDenied. Newer guest
408
+ #: agents say "path escapes the image root"; the "verify ..." forms are what agents already baked into goldens say.
409
+ ROOT_ESCAPE_RE = re.compile(
410
+ r'path escapes the image root|verify "[^"]*": (?:resolved to .*outside the root|too many levels of symbolic links'
411
+ r"|invalid cross-device link|operation not permitted|resolves to a different file)"
412
+ )
413
+
414
+
406
415
  def map_error(exc: BaseException) -> BaseException:
407
416
  """``solari_core`` error -> the Runloop exception a Runloop caller would catch."""
408
417
  if isinstance(exc, ex.RunloopError):
@@ -439,9 +448,18 @@ def map_error(exc: BaseException) -> BaseException:
439
448
  out2.__cause__ = exc
440
449
  return out2
441
450
  if isinstance(exc, core_errors.ConnectionError):
451
+ # The control channel of a devbox that was deleted under a cached handle: the gateway refuses the upgrade with HTTP 404.
452
+ if "websocket" in exc.message.lower() and re.search(r"\bHTTP 404\b", exc.message):
453
+ nf404 = ex.NotFoundError(exc.message, body={"message": exc.message})
454
+ nf404.__cause__ = exc
455
+ return nf404
442
456
  out3 = ex.APIConnectionError(exc.message)
443
457
  out3.__cause__ = exc
444
458
  return out3
459
+ if isinstance(exc, core_errors.ActionError) and str(exc.method or "").startswith("fs.") and ROOT_ESCAPE_RE.search(exc.message or ""):
460
+ out5 = ex.PermissionDeniedError(exc.message, status_code=403, body={"code": "PathOutsideRoot", "error": exc.message})
461
+ out5.__cause__ = exc
462
+ return out5
445
463
  # A file or directory that does not exist is Runloop's 404, not a bare APIError: the guest answers the RPC with "open <path>: no such file
446
464
  # or directory" and no code (fs.download / fs.list / fs.stat ...).
447
465
  if isinstance(exc, core_errors.ActionError) and str(getattr(exc, "method", "")).startswith("fs.") and "no such file or directory" in exc.message.lower():