contree-sdk 0.4.0.dev5__tar.gz → 0.5.0__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 (157) hide show
  1. {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.0}/.gitignore +2 -0
  2. contree_sdk-0.5.0/PKG-INFO +223 -0
  3. contree_sdk-0.5.0/README.md +179 -0
  4. contree_sdk-0.5.0/contree_sdk/__init__.py +39 -0
  5. contree_sdk-0.5.0/contree_sdk/compat.py +12 -0
  6. contree_sdk-0.5.0/contree_sdk/connection.py +165 -0
  7. contree_sdk-0.5.0/contree_sdk/docker/__init__.py +55 -0
  8. contree_sdk-0.5.0/contree_sdk/docker/asyncio.py +195 -0
  9. contree_sdk-0.5.0/contree_sdk/docker/context.py +548 -0
  10. contree_sdk-0.5.0/contree_sdk/docker/copy_layer.py +135 -0
  11. contree_sdk-0.5.0/contree_sdk/docker/copy_source.py +148 -0
  12. contree_sdk-0.5.0/contree_sdk/docker/dockerignore.py +94 -0
  13. contree_sdk-0.5.0/contree_sdk/docker/events.py +83 -0
  14. contree_sdk-0.5.0/contree_sdk/docker/interpolation.py +15 -0
  15. contree_sdk-0.5.0/contree_sdk/docker/keyword.py +45 -0
  16. contree_sdk-0.5.0/contree_sdk/docker/kw_add.py +275 -0
  17. contree_sdk-0.5.0/contree_sdk/docker/kw_arg.py +46 -0
  18. contree_sdk-0.5.0/contree_sdk/docker/kw_copy.py +417 -0
  19. contree_sdk-0.5.0/contree_sdk/docker/kw_env.py +37 -0
  20. contree_sdk-0.5.0/contree_sdk/docker/kw_from.py +185 -0
  21. contree_sdk-0.5.0/contree_sdk/docker/kw_run.py +156 -0
  22. contree_sdk-0.5.0/contree_sdk/docker/kw_skipped.py +30 -0
  23. contree_sdk-0.5.0/contree_sdk/docker/kw_user.py +34 -0
  24. contree_sdk-0.5.0/contree_sdk/docker/kw_workdir.py +45 -0
  25. contree_sdk-0.5.0/contree_sdk/docker/local_context.py +149 -0
  26. contree_sdk-0.5.0/contree_sdk/docker/parser.py +126 -0
  27. contree_sdk-0.5.0/contree_sdk/docker/sync.py +185 -0
  28. contree_sdk-0.5.0/contree_sdk/docker/url_fetch.py +118 -0
  29. contree_sdk-0.5.0/contree_sdk/exceptions.py +24 -0
  30. contree_sdk-0.5.0/contree_sdk/execution.py +160 -0
  31. contree_sdk-0.5.0/contree_sdk/file_io.py +131 -0
  32. contree_sdk-0.5.0/contree_sdk/file_sources.py +144 -0
  33. contree_sdk-0.5.0/contree_sdk/files.py +327 -0
  34. contree_sdk-0.5.0/contree_sdk/images.py +406 -0
  35. contree_sdk-0.5.0/contree_sdk/inspection.py +251 -0
  36. contree_sdk-0.5.0/contree_sdk/langchain/__init__.py +4 -0
  37. contree_sdk-0.5.0/contree_sdk/langchain/sandbox.py +166 -0
  38. contree_sdk-0.5.0/contree_sdk/reuse.py +92 -0
  39. contree_sdk-0.5.0/contree_sdk/sandbox.py +500 -0
  40. contree_sdk-0.5.0/contree_sdk/scope.py +30 -0
  41. contree_sdk-0.5.0/contree_sdk/session/__init__.py +58 -0
  42. contree_sdk-0.5.0/contree_sdk/session/asyncio.py +956 -0
  43. contree_sdk-0.5.0/contree_sdk/session/base.py +93 -0
  44. contree_sdk-0.5.0/contree_sdk/session/cleanup.py +83 -0
  45. contree_sdk-0.5.0/contree_sdk/session/commit_policy.py +34 -0
  46. contree_sdk-0.5.0/contree_sdk/session/contracts.py +204 -0
  47. contree_sdk-0.5.0/contree_sdk/session/detached.py +71 -0
  48. contree_sdk-0.5.0/contree_sdk/session/inspection.py +299 -0
  49. contree_sdk-0.5.0/contree_sdk/session/lazy_async.py +655 -0
  50. contree_sdk-0.5.0/contree_sdk/session/lazy_state.py +95 -0
  51. contree_sdk-0.5.0/contree_sdk/session/lazy_sync.py +627 -0
  52. contree_sdk-0.5.0/contree_sdk/session/live_files.py +53 -0
  53. contree_sdk-0.5.0/contree_sdk/session/models.py +26 -0
  54. contree_sdk-0.5.0/contree_sdk/session/operation_async.py +506 -0
  55. contree_sdk-0.5.0/contree_sdk/session/operation_sync.py +462 -0
  56. contree_sdk-0.5.0/contree_sdk/session/records.py +71 -0
  57. contree_sdk-0.5.0/contree_sdk/session/snapshot_policy.py +266 -0
  58. contree_sdk-0.5.0/contree_sdk/session/stdin.py +39 -0
  59. contree_sdk-0.5.0/contree_sdk/session/subprocess_result.py +157 -0
  60. contree_sdk-0.5.0/contree_sdk/session/sync.py +867 -0
  61. contree_sdk-0.5.0/contree_sdk/session/tree.py +91 -0
  62. contree_sdk-0.5.0/contree_sdk/store/__init__.py +21 -0
  63. contree_sdk-0.5.0/contree_sdk/store/base.py +156 -0
  64. contree_sdk-0.5.0/contree_sdk/store/memory.py +120 -0
  65. contree_sdk-0.5.0/contree_sdk/store/sqlite.py +477 -0
  66. contree_sdk-0.5.0/contree_sdk/store/tree.py +101 -0
  67. contree_sdk-0.5.0/contree_sdk/store/ttl.py +18 -0
  68. contree_sdk-0.5.0/contree_sdk/upload_cache.py +206 -0
  69. contree_sdk-0.5.0/contree_sdk/utils/models/file.py +140 -0
  70. {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.0}/pyproject.toml +25 -38
  71. contree_sdk-0.4.0.dev5/PKG-INFO +0 -590
  72. contree_sdk-0.4.0.dev5/README.md +0 -524
  73. contree_sdk-0.4.0.dev5/contree_sdk/__init__.py +0 -5
  74. contree_sdk-0.4.0.dev5/contree_sdk/_internals/README.md +0 -17
  75. contree_sdk-0.4.0.dev5/contree_sdk/_internals/client/provider.py +0 -138
  76. contree_sdk-0.4.0.dev5/contree_sdk/_internals/client/transport.py +0 -124
  77. contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/codecs.py +0 -28
  78. contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/operation_waiter.py +0 -289
  79. contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/typing.py +0 -66
  80. contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/wiring.py +0 -107
  81. contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/writer_wrapper.py +0 -85
  82. contree_sdk-0.4.0.dev5/contree_sdk/_internals/lib/__init__.py +0 -0
  83. contree_sdk-0.4.0.dev5/contree_sdk/_internals/lib/helpers.py +0 -33
  84. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/__init__.py +0 -0
  85. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/auth_ini.py +0 -51
  86. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/circuit_retrier.py +0 -224
  87. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/config.py +0 -34
  88. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/deprecation.py +0 -58
  89. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/exception.py +0 -72
  90. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/other.py +0 -15
  91. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/typing.py +0 -30
  92. contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/wrapper.py +0 -140
  93. contree_sdk-0.4.0.dev5/contree_sdk/auth.py +0 -72
  94. contree_sdk-0.4.0.dev5/contree_sdk/config.py +0 -54
  95. contree_sdk-0.4.0.dev5/contree_sdk/langchain/__init__.py +0 -0
  96. contree_sdk-0.4.0.dev5/contree_sdk/langchain/sandbox.py +0 -76
  97. contree_sdk-0.4.0.dev5/contree_sdk/sdk/__init__.py +0 -0
  98. contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/__init__.py +0 -5
  99. contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_async.py +0 -42
  100. contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_base.py +0 -196
  101. contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_sync.py +0 -45
  102. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/__init__.py +0 -53
  103. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/api.py +0 -77
  104. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/base.py +0 -16
  105. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/image.py +0 -44
  106. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/operation.py +0 -27
  107. contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/other.py +0 -8
  108. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/__init__.py +0 -0
  109. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/_base.py +0 -6
  110. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/__init__.py +0 -5
  111. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_async.py +0 -5
  112. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_base.py +0 -40
  113. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_sync.py +0 -10
  114. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/__init__.py +0 -5
  115. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_async.py +0 -46
  116. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_base.py +0 -328
  117. contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_sync.py +0 -46
  118. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/__init__.py +0 -0
  119. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/__init__.py +0 -5
  120. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_async.py +0 -8
  121. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_base.py +0 -4
  122. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_sync.py +0 -8
  123. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/__init__.py +0 -10
  124. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_async.py +0 -21
  125. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_base.py +0 -40
  126. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_sync.py +0 -22
  127. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/__init__.py +0 -0
  128. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_async.py +0 -115
  129. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_base.py +0 -500
  130. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_sync.py +0 -184
  131. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/result.py +0 -40
  132. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/state.py +0 -81
  133. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/run.py +0 -43
  134. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/__init__.py +0 -5
  135. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_async.py +0 -5
  136. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_base.py +0 -16
  137. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_sync.py +0 -5
  138. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/__init__.py +0 -6
  139. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_async.py +0 -24
  140. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_base.py +0 -72
  141. contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_sync.py +0 -29
  142. contree_sdk-0.4.0.dev5/contree_sdk/shell/__init__.py +0 -0
  143. contree_sdk-0.4.0.dev5/contree_sdk/utils/__init__.py +0 -0
  144. contree_sdk-0.4.0.dev5/contree_sdk/utils/io.py +0 -26
  145. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/__init__.py +0 -0
  146. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/auth.py +0 -10
  147. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/file.py +0 -62
  148. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/image.py +0 -6
  149. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/operation.py +0 -26
  150. contree_sdk-0.4.0.dev5/contree_sdk/utils/models/stream.py +0 -16
  151. contree_sdk-0.4.0.dev5/contree_sdk/utils/oci.py +0 -49
  152. {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.0}/LICENSE +0 -0
  153. {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.0}/SECURITY.md +0 -0
  154. {contree_sdk-0.4.0.dev5/contree_sdk/_internals → contree_sdk-0.5.0/contree_sdk/shell}/__init__.py +0 -0
  155. {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.0}/contree_sdk/shell/__main__.py +0 -0
  156. {contree_sdk-0.4.0.dev5/contree_sdk/_internals/client → contree_sdk-0.5.0/contree_sdk/utils}/__init__.py +0 -0
  157. {contree_sdk-0.4.0.dev5/contree_sdk/_internals/io → contree_sdk-0.5.0/contree_sdk/utils/models}/__init__.py +0 -0
@@ -143,6 +143,8 @@ pip-log.txt
143
143
  pip-delete-this-directory.txt
144
144
 
145
145
  # Unit test / coverage reports
146
+ /report.html
147
+ /assets/style.css
146
148
  htmlcov/
147
149
  .tox/
148
150
  .nox/
@@ -0,0 +1,223 @@
1
+ Metadata-Version: 2.5
2
+ Name: contree-sdk
3
+ Version: 0.5.0
4
+ Summary: SDK for ConTree container runtime with versioned filesystem state
5
+ License: Apache-2.0
6
+ License-File: LICENSE
7
+ Classifier: Development Status :: 2 - Pre-Alpha
8
+ Classifier: License :: OSI Approved :: Apache Software License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Requires-Python: <3.15,>=3.10
16
+ Requires-Dist: contree-client[httpx]>=0.5.3
17
+ Requires-Dist: typing-extensions; python_version < '3.11'
18
+ Provides-Extra: async
19
+ Requires-Dist: aiosqlite>=0.20; extra == 'async'
20
+ Requires-Dist: contree-client[aiohttp]; extra == 'async'
21
+ Provides-Extra: dev
22
+ Requires-Dist: aiosqlite>=0.20; extra == 'dev'
23
+ Requires-Dist: contree-client[aiohttp]; extra == 'dev'
24
+ Requires-Dist: deepagents==0.7.10; (python_version >= '3.11') and extra == 'dev'
25
+ Requires-Dist: dprint-py; extra == 'dev'
26
+ Requires-Dist: langchain-tests>=1.1.9; (python_version >= '3.11') and extra == 'dev'
27
+ Requires-Dist: markdown-pytest[async]>=0.6.6; extra == 'dev'
28
+ Requires-Dist: pytest-asyncio; extra == 'dev'
29
+ Requires-Dist: pytest-cov; extra == 'dev'
30
+ Requires-Dist: pytest-html; extra == 'dev'
31
+ Requires-Dist: pytest-mock; extra == 'dev'
32
+ Requires-Dist: pytest-xdist; extra == 'dev'
33
+ Requires-Dist: pytest>=8.4.1; extra == 'dev'
34
+ Requires-Dist: rich; extra == 'dev'
35
+ Requires-Dist: ruff; extra == 'dev'
36
+ Requires-Dist: tomli; (python_version < '3.11') and extra == 'dev'
37
+ Requires-Dist: typing-extensions; extra == 'dev'
38
+ Requires-Dist: ty~=0.0.51; extra == 'dev'
39
+ Provides-Extra: docs
40
+ Requires-Dist: contree-docs; extra == 'docs'
41
+ Provides-Extra: langchain
42
+ Requires-Dist: deepagents==0.7.10; (python_version >= '3.11') and extra == 'langchain'
43
+ Description-Content-Type: text/markdown
44
+
45
+ # ConTree SDK
46
+
47
+ [![PyPI version](https://img.shields.io/pypi/v/contree-sdk.svg)](https://pypi.org/project/contree-sdk/)
48
+ [![Python](https://img.shields.io/pypi/pyversions/contree-sdk.svg)](https://pypi.org/project/contree-sdk/)
49
+
50
+ Use one sandbox session to run commands, read and write files, search contents,
51
+ and list directories. Save the filesystem as an image when you need to continue
52
+ later or try a different approach.
53
+
54
+ This checkout contains the breaking **0.5 development API**. Install it locally:
55
+
56
+ ```bash
57
+ pip install -e .
58
+ # Include async transport and SQLite support when needed:
59
+ pip install -e ".[async]"
60
+ ```
61
+
62
+ ## Use a sandbox session
63
+
64
+ The five tools are `run`, `read_file`, `write_file`, `grep`, and `list_dir`.
65
+ Initialize the session [from a token](docs/python_sdk/getting-started.md#create-a-session-from-a-token)
66
+ or [from a saved profile](docs/python_sdk/getting-started.md#create-a-session-from-a-saved-profile).
67
+ Use an image available on your endpoint.
68
+
69
+ ### Sync
70
+
71
+ <!-- name: test_readme; fixtures: sandbox_tools_api, capsys -->
72
+
73
+ ```python
74
+ """Use the five sandbox tools in one session."""
75
+
76
+ from contree_sdk import create_sandbox_session
77
+
78
+
79
+ def main() -> None:
80
+ with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
81
+ # Write a file in the sandbox.
82
+ sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
83
+
84
+ # Run a command and print its output.
85
+ result = sandbox_session.run("cat", args=["/work/message.txt"])
86
+ print(result.stdout)
87
+
88
+ # Read the file as bytes.
89
+ content = sandbox_session.read_file("/work/message.txt")
90
+ print(content.decode())
91
+
92
+ # Search file contents.
93
+ matches = sandbox_session.grep("Hello", path="/work/message.txt")
94
+ print(matches)
95
+
96
+ # List the directory with file metadata.
97
+ directory = sandbox_session.list_dir("/work")
98
+ print(directory)
99
+
100
+
101
+ main()
102
+ ```
103
+
104
+ <!--
105
+ name: test_readme
106
+ ```python
107
+ assert (
108
+ capsys.readouterr().out
109
+ == "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
110
+ )
111
+ client = sandbox_tools_api.sync
112
+ assert len(client.calls_for("spawn_instance")) == 1
113
+ assert len(client.calls_for("operation_subprocess_create")) == 2
114
+ assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
115
+ assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
116
+ assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
117
+ assert len(client.calls_for("operation_subprocess_kill")) == 1
118
+ ```
119
+ -->
120
+
121
+ ### Async
122
+
123
+ <!-- name: test_readme_async; fixtures: sandbox_tools_api, capsys -->
124
+
125
+ ```python
126
+ import asyncio
127
+
128
+ from contree_sdk import create_sandbox_session
129
+
130
+
131
+ async def main() -> None:
132
+ async with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
133
+ # Write a file in the sandbox.
134
+ await sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
135
+
136
+ # Run a command and print its output.
137
+ result = await sandbox_session.run("cat", args=["/work/message.txt"])
138
+ print(result.stdout)
139
+
140
+ # Read the file as bytes.
141
+ content = await sandbox_session.read_file("/work/message.txt")
142
+ print(content.decode())
143
+
144
+ # Search file contents.
145
+ matches = await sandbox_session.grep("Hello", path="/work/message.txt")
146
+ print(matches)
147
+
148
+ # List the directory with file metadata.
149
+ directory = await sandbox_session.list_dir("/work")
150
+ print(directory)
151
+
152
+
153
+ asyncio.run(main())
154
+ ```
155
+
156
+ <!--
157
+ name: test_readme_async
158
+ ```python
159
+ assert (
160
+ capsys.readouterr().out
161
+ == "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
162
+ )
163
+ client = sandbox_tools_api.async_client
164
+ assert len(client.calls_for("spawn_instance")) == 1
165
+ assert len(client.calls_for("operation_subprocess_create")) == 2
166
+ assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
167
+ assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
168
+ assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
169
+ assert len(client.calls_for("operation_subprocess_kill")) == 1
170
+ ```
171
+ -->
172
+
173
+ The command and file read both print `Hello from ConTree!`. The search prints
174
+ `/work/message.txt:1:Hello from ConTree!`, and the listing shows metadata for `message.txt`.
175
+
176
+ The default `LazySession` (`AsyncLazySession` for async code) starts a VM on the
177
+ first command and reuses it for later commands. After 10 idle seconds, it saves
178
+ the filesystem and stops the VM automatically. The next command starts a new VM
179
+ from those saved files. Processes and memory do not survive a snapshot.
180
+ Normal context exit also saves pending changes.
181
+
182
+ To save after each command, select `session_type=ContreeSession` for Sync or
183
+ `session_type=ContreeAsyncSession` for Async. Each command then uses a separate
184
+ VM. See the complete [save-after-every-command examples](docs/python_sdk/getting-started.md#save-after-every-command).
185
+
186
+ ## Follow a workflow
187
+
188
+ | Task | Guide |
189
+ | ------------------------------------------------ | ---------------------------------------------------------- |
190
+ | Configure credentials and use sync or async code | [Your first sandbox](docs/python_sdk/getting-started.md) |
191
+ | Import a Docker image | [Import a Docker image](docs/python_sdk/images.md) |
192
+ | Build from a Dockerfile | [Build a Docker image](docs/python_sdk/building-images.md) |
193
+ | Select components and control their behavior | [Advanced usage](docs/python_sdk/advanced.md) |
194
+ | Upload inputs and download results | [Files](docs/python_sdk/files.md) |
195
+ | Continue after restarting Python | [Save and resume](docs/python_sdk/sessions.md) |
196
+ | Try alternatives from a checkpoint | [Branches](docs/python_sdk/branching.md) |
197
+ | Control a running sandbox | [Operations](docs/python_sdk/operation.md) |
198
+ | Connect deepagents | [Agent integration](docs/integrations/langchain.md) |
199
+ | Override policy or replace components | [Customization](docs/python_sdk/customization.md) |
200
+ | Upgrade from the old SDK | [Migration](docs/python_sdk/migration.md) |
201
+
202
+ The [documentation](https://nebius.github.io/contree-sdk/) includes API references and
203
+ failure-handling examples. The mini-swe-agent 2.4.6 bundled adapter needs a port
204
+ before it can use this API; see the [compatibility note](docs/integrations/mini-swe-agent.md).
205
+
206
+ ## Develop and test
207
+
208
+ ```bash
209
+ uv sync --extra dev --extra docs
210
+ uv run pytest
211
+ ```
212
+
213
+ The regular test run executes the Python scenarios in README and `docs/` through
214
+ [markdown-pytest](https://mosquito.github.io/markdown-pytest/). The SDK and stores run
215
+ normally; fixtures supply transport responses. No server credentials are required.
216
+ See [contributing](docs/contributing.md) for checks and how to add a scenario.
217
+
218
+ Licensed under [Apache 2.0](LICENSE). Report vulnerabilities through the process
219
+ in [SECURITY.md](SECURITY.md).
220
+
221
+ Standalone programs with embedded tests are in [`examples/sync/`](examples/sync/)
222
+ and [`examples/async/`](examples/async/). See the [examples guide](examples/README.md)
223
+ for prerequisites and execution commands.
@@ -0,0 +1,179 @@
1
+ # ConTree SDK
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/contree-sdk.svg)](https://pypi.org/project/contree-sdk/)
4
+ [![Python](https://img.shields.io/pypi/pyversions/contree-sdk.svg)](https://pypi.org/project/contree-sdk/)
5
+
6
+ Use one sandbox session to run commands, read and write files, search contents,
7
+ and list directories. Save the filesystem as an image when you need to continue
8
+ later or try a different approach.
9
+
10
+ This checkout contains the breaking **0.5 development API**. Install it locally:
11
+
12
+ ```bash
13
+ pip install -e .
14
+ # Include async transport and SQLite support when needed:
15
+ pip install -e ".[async]"
16
+ ```
17
+
18
+ ## Use a sandbox session
19
+
20
+ The five tools are `run`, `read_file`, `write_file`, `grep`, and `list_dir`.
21
+ Initialize the session [from a token](docs/python_sdk/getting-started.md#create-a-session-from-a-token)
22
+ or [from a saved profile](docs/python_sdk/getting-started.md#create-a-session-from-a-saved-profile).
23
+ Use an image available on your endpoint.
24
+
25
+ ### Sync
26
+
27
+ <!-- name: test_readme; fixtures: sandbox_tools_api, capsys -->
28
+
29
+ ```python
30
+ """Use the five sandbox tools in one session."""
31
+
32
+ from contree_sdk import create_sandbox_session
33
+
34
+
35
+ def main() -> None:
36
+ with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
37
+ # Write a file in the sandbox.
38
+ sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
39
+
40
+ # Run a command and print its output.
41
+ result = sandbox_session.run("cat", args=["/work/message.txt"])
42
+ print(result.stdout)
43
+
44
+ # Read the file as bytes.
45
+ content = sandbox_session.read_file("/work/message.txt")
46
+ print(content.decode())
47
+
48
+ # Search file contents.
49
+ matches = sandbox_session.grep("Hello", path="/work/message.txt")
50
+ print(matches)
51
+
52
+ # List the directory with file metadata.
53
+ directory = sandbox_session.list_dir("/work")
54
+ print(directory)
55
+
56
+
57
+ main()
58
+ ```
59
+
60
+ <!--
61
+ name: test_readme
62
+ ```python
63
+ assert (
64
+ capsys.readouterr().out
65
+ == "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
66
+ )
67
+ client = sandbox_tools_api.sync
68
+ assert len(client.calls_for("spawn_instance")) == 1
69
+ assert len(client.calls_for("operation_subprocess_create")) == 2
70
+ assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
71
+ assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
72
+ assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
73
+ assert len(client.calls_for("operation_subprocess_kill")) == 1
74
+ ```
75
+ -->
76
+
77
+ ### Async
78
+
79
+ <!-- name: test_readme_async; fixtures: sandbox_tools_api, capsys -->
80
+
81
+ ```python
82
+ import asyncio
83
+
84
+ from contree_sdk import create_sandbox_session
85
+
86
+
87
+ async def main() -> None:
88
+ async with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
89
+ # Write a file in the sandbox.
90
+ await sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
91
+
92
+ # Run a command and print its output.
93
+ result = await sandbox_session.run("cat", args=["/work/message.txt"])
94
+ print(result.stdout)
95
+
96
+ # Read the file as bytes.
97
+ content = await sandbox_session.read_file("/work/message.txt")
98
+ print(content.decode())
99
+
100
+ # Search file contents.
101
+ matches = await sandbox_session.grep("Hello", path="/work/message.txt")
102
+ print(matches)
103
+
104
+ # List the directory with file metadata.
105
+ directory = await sandbox_session.list_dir("/work")
106
+ print(directory)
107
+
108
+
109
+ asyncio.run(main())
110
+ ```
111
+
112
+ <!--
113
+ name: test_readme_async
114
+ ```python
115
+ assert (
116
+ capsys.readouterr().out
117
+ == "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
118
+ )
119
+ client = sandbox_tools_api.async_client
120
+ assert len(client.calls_for("spawn_instance")) == 1
121
+ assert len(client.calls_for("operation_subprocess_create")) == 2
122
+ assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
123
+ assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
124
+ assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
125
+ assert len(client.calls_for("operation_subprocess_kill")) == 1
126
+ ```
127
+ -->
128
+
129
+ The command and file read both print `Hello from ConTree!`. The search prints
130
+ `/work/message.txt:1:Hello from ConTree!`, and the listing shows metadata for `message.txt`.
131
+
132
+ The default `LazySession` (`AsyncLazySession` for async code) starts a VM on the
133
+ first command and reuses it for later commands. After 10 idle seconds, it saves
134
+ the filesystem and stops the VM automatically. The next command starts a new VM
135
+ from those saved files. Processes and memory do not survive a snapshot.
136
+ Normal context exit also saves pending changes.
137
+
138
+ To save after each command, select `session_type=ContreeSession` for Sync or
139
+ `session_type=ContreeAsyncSession` for Async. Each command then uses a separate
140
+ VM. See the complete [save-after-every-command examples](docs/python_sdk/getting-started.md#save-after-every-command).
141
+
142
+ ## Follow a workflow
143
+
144
+ | Task | Guide |
145
+ | ------------------------------------------------ | ---------------------------------------------------------- |
146
+ | Configure credentials and use sync or async code | [Your first sandbox](docs/python_sdk/getting-started.md) |
147
+ | Import a Docker image | [Import a Docker image](docs/python_sdk/images.md) |
148
+ | Build from a Dockerfile | [Build a Docker image](docs/python_sdk/building-images.md) |
149
+ | Select components and control their behavior | [Advanced usage](docs/python_sdk/advanced.md) |
150
+ | Upload inputs and download results | [Files](docs/python_sdk/files.md) |
151
+ | Continue after restarting Python | [Save and resume](docs/python_sdk/sessions.md) |
152
+ | Try alternatives from a checkpoint | [Branches](docs/python_sdk/branching.md) |
153
+ | Control a running sandbox | [Operations](docs/python_sdk/operation.md) |
154
+ | Connect deepagents | [Agent integration](docs/integrations/langchain.md) |
155
+ | Override policy or replace components | [Customization](docs/python_sdk/customization.md) |
156
+ | Upgrade from the old SDK | [Migration](docs/python_sdk/migration.md) |
157
+
158
+ The [documentation](https://nebius.github.io/contree-sdk/) includes API references and
159
+ failure-handling examples. The mini-swe-agent 2.4.6 bundled adapter needs a port
160
+ before it can use this API; see the [compatibility note](docs/integrations/mini-swe-agent.md).
161
+
162
+ ## Develop and test
163
+
164
+ ```bash
165
+ uv sync --extra dev --extra docs
166
+ uv run pytest
167
+ ```
168
+
169
+ The regular test run executes the Python scenarios in README and `docs/` through
170
+ [markdown-pytest](https://mosquito.github.io/markdown-pytest/). The SDK and stores run
171
+ normally; fixtures supply transport responses. No server credentials are required.
172
+ See [contributing](docs/contributing.md) for checks and how to add a scenario.
173
+
174
+ Licensed under [Apache 2.0](LICENSE). Report vulnerabilities through the process
175
+ in [SECURITY.md](SECURITY.md).
176
+
177
+ Standalone programs with embedded tests are in [`examples/sync/`](examples/sync/)
178
+ and [`examples/async/`](examples/async/). See the [examples guide](examples/README.md)
179
+ for prerequisites and execution commands.
@@ -0,0 +1,39 @@
1
+ from contree_sdk.connection import profile_from_args
2
+ from contree_sdk.execution import AsyncExecutor, OperationContext, RunRequest, SyncExecutor
3
+ from contree_sdk.images import async_build_image, async_import_image, sync_build_image, sync_import_image
4
+ from contree_sdk.inspection import AsyncImageInspector, ImageInspector
5
+ from contree_sdk.sandbox import (
6
+ SandboxSessionContext,
7
+ create_async_sandbox_session,
8
+ create_sandbox_session,
9
+ create_sync_sandbox_session,
10
+ session_maker,
11
+ )
12
+ from contree_sdk.session import AsyncLazySession, ContreeAsyncSession, ContreeSession, LazySession
13
+ from contree_sdk.session.inspection import AsyncSessionInspector, SessionInspector
14
+
15
+
16
+ __all__ = [
17
+ "AsyncExecutor",
18
+ "AsyncImageInspector",
19
+ "AsyncLazySession",
20
+ "AsyncSessionInspector",
21
+ "ContreeAsyncSession",
22
+ "ContreeSession",
23
+ "ImageInspector",
24
+ "LazySession",
25
+ "OperationContext",
26
+ "RunRequest",
27
+ "SandboxSessionContext",
28
+ "SessionInspector",
29
+ "SyncExecutor",
30
+ "async_build_image",
31
+ "async_import_image",
32
+ "create_async_sandbox_session",
33
+ "create_sandbox_session",
34
+ "create_sync_sandbox_session",
35
+ "profile_from_args",
36
+ "session_maker",
37
+ "sync_build_image",
38
+ "sync_import_image",
39
+ ]
@@ -0,0 +1,12 @@
1
+ """Imports that differ between supported Python versions."""
2
+
3
+ import sys
4
+
5
+
6
+ if sys.version_info >= (3, 11):
7
+ from typing import Self
8
+ else:
9
+ from typing_extensions import Self
10
+
11
+
12
+ __all__ = ["Self"]
@@ -0,0 +1,165 @@
1
+ """Shared connection resolution and ownership for public SDK factories."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from collections.abc import AsyncIterator, Iterator
7
+ from contextlib import AsyncExitStack, ExitStack, asynccontextmanager, contextmanager, suppress
8
+ from dataclasses import dataclass, field
9
+ from pathlib import Path
10
+ from typing import Any, Literal, cast
11
+
12
+ from contree_client import RetryPolicy, profiles
13
+ from contree_client.profiles import Profile
14
+ from contree_client.types import ContreeAsyncClient, ContreeSyncClient
15
+
16
+ from contree_sdk.session.cleanup import await_cleanup
17
+
18
+
19
+ def profile_from_args(
20
+ *, token: str, url: str | None = None, project: str | None = None, auth_type: Literal["iam", "jwt"] = "iam"
21
+ ) -> Profile:
22
+ """Build an explicit client profile without reading or writing configuration.
23
+
24
+ The default configuration uses the client's endpoint when URL is omitted.
25
+
26
+ Returns:
27
+ A regular contree-client Profile.
28
+
29
+ Raises:
30
+ ValueError: Credentials, URL, or authentication type are invalid.
31
+
32
+ """
33
+ if auth_type not in {"iam", "jwt"}:
34
+ raise ValueError("auth_type must be 'iam' or 'jwt'")
35
+ if not token or not token.strip():
36
+ raise ValueError("token must not be empty")
37
+ if url is None and auth_type == "iam":
38
+ url = profiles.DEFAULT_IAM_URL
39
+ if not url or not url.strip():
40
+ raise ValueError("url is required for JWT authentication")
41
+ return Profile(name="explicit", url=url, token=token, project=project, auth_type=auth_type)
42
+
43
+
44
+ @dataclass
45
+ class ConnectionConfig:
46
+ url: str | None = None
47
+ project: str | None = None
48
+ token: str | None = field(default=None, repr=False)
49
+ auth_type: Literal["iam", "jwt"] | None = None
50
+ profile: Profile | str | None = None
51
+ config_path: str | Path | None = None
52
+ client: ContreeSyncClient | ContreeAsyncClient | None = None
53
+ client_type: type[ContreeSyncClient | ContreeAsyncClient] | None = None
54
+ client_kwargs: dict[str, Any] = field(default_factory=dict, repr=False)
55
+
56
+ def resolve_profile(self) -> Profile:
57
+ if any(value is not None for value in (self.url, self.project, self.token, self.auth_type)):
58
+ if self.profile is not None or self.config_path is not None:
59
+ raise ValueError("connection arguments cannot be combined with profile or config_path")
60
+ return profile_from_args(
61
+ token=self.token or "", url=self.url, project=self.project, auth_type=self.auth_type or "iam"
62
+ )
63
+ if isinstance(self.profile, Profile):
64
+ if self.config_path is not None:
65
+ raise ValueError("a Profile object cannot be combined with config_path")
66
+ return self.profile
67
+ if self.profile is not None or self.config_path is not None:
68
+ return profiles.resolve_profile(self.profile, path=self.config_path)
69
+ return profiles.from_env() or profiles.resolve_profile()
70
+
71
+ def validate(self, *, asynchronous: bool) -> None:
72
+ mode = "async with" if asynchronous else "with"
73
+ contract = ContreeAsyncClient if asynchronous else ContreeSyncClient
74
+ if self.client_type is not None and (
75
+ not isinstance(self.client_type, type) or not issubclass(self.client_type, contract)
76
+ ):
77
+ raise TypeError(f"client_type must be a compatible client class for {mode}")
78
+ if self.client is not None and not isinstance(self.client, contract):
79
+ raise TypeError(f"client must implement the client interface for {mode}")
80
+ if self.client is not None and (
81
+ self.client_kwargs
82
+ or any(
83
+ value is not None
84
+ for value in (
85
+ self.client_type,
86
+ self.profile,
87
+ self.config_path,
88
+ self.url,
89
+ self.project,
90
+ self.token,
91
+ self.auth_type,
92
+ )
93
+ )
94
+ ):
95
+ raise ValueError("client cannot be combined with client_type or connection settings")
96
+ if "base_url" in self.client_kwargs:
97
+ raise ValueError("use url or profile instead of base_url")
98
+
99
+ def open_sync(self, stack: ExitStack) -> ContreeSyncClient:
100
+ self.validate(asynchronous=False)
101
+ if self.client is not None:
102
+ return cast("ContreeSyncClient", self.client)
103
+ client_type = cast("type[ContreeSyncClient] | None", self.client_type)
104
+ if client_type is None:
105
+ from contree_client.httpx import ContreeClient
106
+
107
+ client_type = ContreeClient
108
+ kwargs: dict[str, Any] = {"retry": RetryPolicy(), **self.client_kwargs}
109
+ client = client_type.from_profile(self.resolve_profile(), **kwargs)
110
+ stack.callback(client.close)
111
+ client.open()
112
+ return client
113
+
114
+ async def open_async(self, stack: AsyncExitStack) -> ContreeAsyncClient:
115
+ self.validate(asynchronous=True)
116
+ if self.client is not None:
117
+ return cast("ContreeAsyncClient", self.client)
118
+ client_type = cast("type[ContreeAsyncClient] | None", self.client_type)
119
+ if client_type is None:
120
+ from contree_client.aiohttp import ContreeAsyncClient
121
+
122
+ client_type = ContreeAsyncClient
123
+ kwargs: dict[str, Any] = {"retry": RetryPolicy(), **self.client_kwargs}
124
+ client = client_type.from_profile(self.resolve_profile(), **kwargs)
125
+ stack.push_async_callback(client.close)
126
+ await client.open()
127
+ return client
128
+
129
+
130
+ @contextmanager
131
+ def owned_resources() -> Iterator[ExitStack]:
132
+ """Close resources while preserving the primary error.
133
+
134
+ Yields:
135
+ A stack for resources created by a helper.
136
+
137
+ """
138
+ stack = ExitStack()
139
+ try:
140
+ yield stack
141
+ except BaseException:
142
+ with suppress(Exception):
143
+ stack.close()
144
+ raise
145
+ else:
146
+ stack.close()
147
+
148
+
149
+ @asynccontextmanager
150
+ async def owned_resources_async() -> AsyncIterator[AsyncExitStack]:
151
+ """Join cleanup despite repeated cancellation.
152
+
153
+ Yields:
154
+ A stack for resources created by an async helper.
155
+
156
+ """
157
+ stack = AsyncExitStack()
158
+ try:
159
+ yield stack
160
+ except BaseException:
161
+ with suppress(Exception, asyncio.CancelledError):
162
+ await await_cleanup(stack.aclose())
163
+ raise
164
+ else:
165
+ await await_cleanup(stack.aclose())
@@ -0,0 +1,55 @@
1
+ """Dockerfile parser and keyword interpreters.
2
+
3
+ Each Dockerfile directive is a `DockerKeyword` subclass living in its own
4
+ module. `parse_dockerfile` returns a list of directives ready to be executed
5
+ against a `BuildContext`/`AsyncBuildContext`.
6
+ """
7
+
8
+ from .asyncio import ContreeAsyncDockerBuilder
9
+ from .context import AsyncBuildContext, BuildContext, BuildRequest, BuildStepEvent, PendingFile
10
+ from .dockerignore import DockerignoreRule, is_ignored, parse_dockerignore
11
+ from .events import BuildEvent, BuildEventType, BuildStep
12
+ from .keyword import DockerKeyword, substitute
13
+ from .kw_add import AddKeyword
14
+ from .kw_arg import ArgKeyword
15
+ from .kw_copy import CopyKeyword
16
+ from .kw_env import EnvKeyword
17
+ from .kw_from import FromKeyword
18
+ from .kw_run import RunKeyword
19
+ from .kw_skipped import SkippedKeyword
20
+ from .kw_user import UserKeyword
21
+ from .kw_workdir import WorkdirKeyword
22
+ from .local_context import LocalContext
23
+ from .parser import DockerfileParser, parse_dockerfile
24
+ from .sync import ContreeDockerBuilder
25
+
26
+
27
+ __all__ = [
28
+ "AddKeyword",
29
+ "ArgKeyword",
30
+ "AsyncBuildContext",
31
+ "BuildContext",
32
+ "BuildEvent",
33
+ "BuildEventType",
34
+ "BuildRequest",
35
+ "BuildStep",
36
+ "BuildStepEvent",
37
+ "ContreeAsyncDockerBuilder",
38
+ "ContreeDockerBuilder",
39
+ "CopyKeyword",
40
+ "DockerKeyword",
41
+ "DockerfileParser",
42
+ "DockerignoreRule",
43
+ "EnvKeyword",
44
+ "FromKeyword",
45
+ "LocalContext",
46
+ "PendingFile",
47
+ "RunKeyword",
48
+ "SkippedKeyword",
49
+ "UserKeyword",
50
+ "WorkdirKeyword",
51
+ "is_ignored",
52
+ "parse_dockerfile",
53
+ "parse_dockerignore",
54
+ "substitute",
55
+ ]