@yaks/sandbox 0.0.0-stage → 0.2.36
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.
- package/LICENSE +202 -0
- package/README.md +400 -2
- package/_dist/bubblewrap.d.ts +38 -0
- package/_dist/bubblewrap.d.ts.map +1 -0
- package/_dist/cloudflare.d.ts +136 -0
- package/_dist/cloudflare.d.ts.map +1 -0
- package/_dist/configuration.d.ts +13 -0
- package/_dist/configuration.d.ts.map +1 -0
- package/_dist/docker.d.ts +15 -0
- package/_dist/docker.d.ts.map +1 -0
- package/_dist/effects.d.ts +6 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/lifecycle.d.ts +38 -0
- package/_dist/lifecycle.d.ts.map +1 -0
- package/_dist/mod.d.ts +4 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/provider.d.ts +33 -0
- package/_dist/provider.d.ts.map +1 -0
- package/_dist/pve.d.ts +17 -0
- package/_dist/pve.d.ts.map +1 -0
- package/_dist/recovery.d.ts +71 -0
- package/_dist/recovery.d.ts.map +1 -0
- package/_dist/tools.d.ts +4 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +5 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/archive.js +60 -0
- package/archive.js.map +1 -0
- package/archive.ts +49 -0
- package/bubblewrap.js +495 -0
- package/bubblewrap.js.map +1 -0
- package/bubblewrap.ts +555 -0
- package/cloudflare.js +434 -0
- package/cloudflare.js.map +1 -0
- package/cloudflare.ts +592 -0
- package/configuration.js +84 -0
- package/configuration.js.map +1 -0
- package/configuration.ts +120 -0
- package/deno.json +1 -0
- package/docker.js +149 -0
- package/docker.js.map +1 -0
- package/docker.ts +206 -0
- package/effects.js +86 -0
- package/effects.js.map +1 -0
- package/effects.ts +78 -0
- package/lifecycle.js +153 -0
- package/lifecycle.js.map +1 -0
- package/lifecycle.ts +207 -0
- package/mod.js +6 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/package.json +64 -4
- package/provider.js +11 -0
- package/provider.js.map +1 -0
- package/provider.ts +44 -0
- package/pve.js +197 -0
- package/pve.js.map +1 -0
- package/pve.ts +259 -0
- package/recovery-script.js +187 -0
- package/recovery-script.js.map +1 -0
- package/recovery-script.ts +186 -0
- package/recovery.js +231 -0
- package/recovery.js.map +1 -0
- package/recovery.ts +317 -0
- package/sandbox-runner.js +218 -0
- package/sandbox-runner.js.map +1 -0
- package/sandbox-runner.ts +217 -0
- package/tools.js +42 -0
- package/tools.js.map +1 -0
- package/tools.ts +45 -0
- package/vocab.js +13 -0
- package/vocab.js.map +1 -0
- package/vocab.json +180 -0
- package/vocab.ts +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,401 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Sandboxes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Containers and jails recorded separately from computers, with provisioning
|
|
4
|
+
intentions and confirmed disposal in the graph.
|
|
5
|
+
|
|
6
|
+
A **sandbox** is an actual or requested container or jail. Its
|
|
7
|
+
`sandbox{provider, address, from, image}` records the backend and resource
|
|
8
|
+
locator. Its `workspace{machine, root}` records placement and the default
|
|
9
|
+
absolute directory. A sandbox is not a computer; computers and workspace verbs
|
|
10
|
+
belong to [machine](../machine).
|
|
11
|
+
|
|
12
|
+
## Read lifecycle status
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { equal } from '@yaks/testing'
|
|
16
|
+
import { loadVocab } from '@yaks/vocab'
|
|
17
|
+
import { sandboxDoc } from '@yaks/sandbox'
|
|
18
|
+
import { kernelDoc } from '@yaks/kernel'
|
|
19
|
+
|
|
20
|
+
let vocab = loadVocab([kernelDoc, sandboxDoc])
|
|
21
|
+
equal(
|
|
22
|
+
vocab.comp('sandbox')?.ladder?.rungs.find((r) => r.comp == 'pending')?.status,
|
|
23
|
+
'requested',
|
|
24
|
+
)
|
|
25
|
+
equal(
|
|
26
|
+
vocab.comp('sandbox')?.ladder?.rungs.find((r) => r.comp == 'released')
|
|
27
|
+
?.status,
|
|
28
|
+
'released',
|
|
29
|
+
)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`pending{}` means provisioning is requested, `failed{reason}` records a failure,
|
|
33
|
+
and `released{at, by, via}` records that the provider stopped the sandbox and
|
|
34
|
+
retained its files in trash. Without these marks, `sandbox.status` is `ready`;
|
|
35
|
+
readiness does not promise the resource is awake. Providers must check
|
|
36
|
+
availability on connection. Missing computer placement is unknown, never an
|
|
37
|
+
implicit local computer. Changing a session's home does not release a sandbox.
|
|
38
|
+
|
|
39
|
+
## Exports
|
|
40
|
+
|
|
41
|
+
| Export | Offers |
|
|
42
|
+
| -------------- | ------------------------------------------------------------ |
|
|
43
|
+
| `.` | `sandboxDoc`, sandbox provider types, export path validation |
|
|
44
|
+
| `./vocab` | `sandboxDoc`, `docs`, `description` |
|
|
45
|
+
| `./docker` | `dockerSandboxes` |
|
|
46
|
+
| `./pve` | `pveClones` |
|
|
47
|
+
| `./bubblewrap` | `sandboxProvider`, limits and options |
|
|
48
|
+
| `./cloudflare` | `cloudflareProvider`, `hold` and `lending`, SDK types |
|
|
49
|
+
|
|
50
|
+
## Docker Sandboxes
|
|
51
|
+
|
|
52
|
+
A **sandbox provider** creates containers and retains disposed ones in
|
|
53
|
+
recoverable trash on an explicitly selected computer. A **sandbox connection**
|
|
54
|
+
offers workspace verbs for the existing filesystem; connecting never recreates a
|
|
55
|
+
missing sandbox. The provider takes a preallocated sandbox eid, before any
|
|
56
|
+
external action.
|
|
57
|
+
|
|
58
|
+
The `./docker` adapter runs `sbx` through a computer transport. Its local mode
|
|
59
|
+
uses Docker Sandboxes on that computer, not the graph server. No host directory
|
|
60
|
+
is bind-mounted. `sbx exec` wakes existing writable storage. A durable host
|
|
61
|
+
receipt and sandbox ownership marker prevent retries from creating another
|
|
62
|
+
resource or disposing a foreign resource. Keep the host receipt directory for as
|
|
63
|
+
long as sandbox ids can be retried, including after disposal.
|
|
64
|
+
|
|
65
|
+
This example needs a configured SSH computer running authenticated `sbx`:
|
|
66
|
+
|
|
67
|
+
```ts ignore
|
|
68
|
+
import { dockerSandboxes } from '@yaks/sandbox/docker'
|
|
69
|
+
import { sshConnector } from '@yaks/machine/connectors'
|
|
70
|
+
|
|
71
|
+
let transport = await sshConnector().connect('ssh://coder@container-host')
|
|
72
|
+
let provider = dockerSandboxes(graph, {
|
|
73
|
+
transport,
|
|
74
|
+
state: '/home/coder/.local/state/yak-sandboxes',
|
|
75
|
+
})
|
|
76
|
+
let ref = { id: sandboxEid }
|
|
77
|
+
let { workspace } = await provider.request!(ref)
|
|
78
|
+
await workspace.write('hello.txt', 'hello')
|
|
79
|
+
let reconnected = await provider.connect(ref)
|
|
80
|
+
console.log(await reconnected.workspace.read('hello.txt'))
|
|
81
|
+
await provider.dispose(ref)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The default root is `/workspace`; the adapter creates it and assigns its owner
|
|
85
|
+
to the shell template's default user. Templates need bash, base64, realpath,
|
|
86
|
+
setsid, flock, Python 3 and Linux `/proc`. Host control needs bash and flock.
|
|
87
|
+
Commands passed to `sbx` have stdin detached from the control script so an sbx
|
|
88
|
+
invocation cannot consume later lifecycle instructions. `from` requires an
|
|
89
|
+
idempotent `prepare` callback; without one it is refused before provisioning.
|
|
90
|
+
Initial provenance is not a backup. Disposal retains writable storage in trash
|
|
91
|
+
and stops its processes; it is never triggered by a session home change.
|
|
92
|
+
|
|
93
|
+
Exports accept relative names only. They open each path component by directory
|
|
94
|
+
descriptor inside the sandbox root and reject symlinks and special files. The
|
|
95
|
+
adapter does not provide idle eviction, permissions, host installation or a
|
|
96
|
+
local Docker fallback.
|
|
97
|
+
|
|
98
|
+
## Cloudflare containers
|
|
99
|
+
|
|
100
|
+
`cloudflareProvider(namespace, options?)` lends the containers in a deployed
|
|
101
|
+
Cloudflare Sandbox namespace. The request id is the container's name. Request
|
|
102
|
+
sets its idle sleep backstop; connect reaches the same files and processes;
|
|
103
|
+
dispose archives writable storage before stopping the container. The namespace
|
|
104
|
+
chooses its deployed image, so `from`, `image` and existing-sandbox addresses
|
|
105
|
+
are refused rather than ignored.
|
|
106
|
+
|
|
107
|
+
Commands keep completed processes for `look` and `tail`; their ceiling is four
|
|
108
|
+
minutes. SIGTERM and SIGKILL both use the SDK's single kill operation. Export
|
|
109
|
+
reads binary files through the SDK's base64 encoding; relative paths start in
|
|
110
|
+
`/workspace`, and absolute paths are supported for the platform's artifact
|
|
111
|
+
tools.
|
|
112
|
+
|
|
113
|
+
The host supplies `options.env` at command invocation (for example, a scoped
|
|
114
|
+
grant), `options.retry` for transient RPC failures, and `options.sleepError` to
|
|
115
|
+
report a failed idle configuration. This provider knows no account or meter.
|
|
116
|
+
|
|
117
|
+
The namespace in this example is a stand-in for the deployed binding:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { equal } from '@yaks/testing'
|
|
121
|
+
import { type Box, cloudflareProvider } from '@yaks/sandbox/cloudflare'
|
|
122
|
+
|
|
123
|
+
let destroyed = 0, retained = ''
|
|
124
|
+
let provider = cloudflareProvider({
|
|
125
|
+
idFromName: (name) => name,
|
|
126
|
+
get: () =>
|
|
127
|
+
({
|
|
128
|
+
readFile: async () => ({ content: 'unfinished draft' }),
|
|
129
|
+
destroy: async () => {
|
|
130
|
+
destroyed++
|
|
131
|
+
},
|
|
132
|
+
}) as unknown as Box,
|
|
133
|
+
}, {
|
|
134
|
+
// In production this callback runs under a distributed lock and save commits
|
|
135
|
+
// to durable storage outside the container. This example only disposes once.
|
|
136
|
+
trash: async (_ref, box) => {
|
|
137
|
+
// This example has one file. A host archives every editable directory.
|
|
138
|
+
retained = (await box.readFile('/workspace/draft')).content
|
|
139
|
+
await box.destroy()
|
|
140
|
+
},
|
|
141
|
+
lifecycle: {
|
|
142
|
+
locked: async (_id, act) => await act({ save: async () => {} }),
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
await provider.dispose({ id: 'build-example' })
|
|
146
|
+
equal(destroyed, 1)
|
|
147
|
+
equal(retained, 'unfinished draft')
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Cloudflare placement is unknown: the connection supplies a `Workspace` and
|
|
151
|
+
`root`, never an invented computer id. Durable use requires
|
|
152
|
+
`CloudflareOptions.lifecycle.locked`, backed by storage outside the container
|
|
153
|
+
and distributed serialization. The callback's `save` must commit before it
|
|
154
|
+
returns. `connect` refuses stopped/missing storage instead of recreating it;
|
|
155
|
+
`dispose` requires `trash(ref, box)`, which preserves recoverable writable
|
|
156
|
+
storage before it stops the container, then keeps a durable trash receipt.
|
|
157
|
+
`restore(ref, box)` restores that storage when requested explicitly. No callback
|
|
158
|
+
means disposal fails before touching storage.
|
|
159
|
+
|
|
160
|
+
A deployed Cloudflare Container's disk does not outlive its nap. A host using
|
|
161
|
+
`ephemeral: true` supplies `prepare(ref, box)` to restore retained files before
|
|
162
|
+
every public workspace RPC, and preserves them before idle shutdown as well as
|
|
163
|
+
explicit disposal. The host owns this storage boundary; an initial image is
|
|
164
|
+
never a backup of edits.
|
|
165
|
+
|
|
166
|
+
Unlike the common relative-only export contract, Cloudflare also accepts
|
|
167
|
+
absolute export paths for the hosted `sandbox_ship` published API. SDK process
|
|
168
|
+
ids remain scoped to this container; durable graph process-handle migration is
|
|
169
|
+
not supplied by this adapter.
|
|
170
|
+
|
|
171
|
+
### Lent sandboxes
|
|
172
|
+
|
|
173
|
+
A host that lends one container per person within a group (yaks.app lends one
|
|
174
|
+
per member of a space) names it `named(group, person)` and keeps who has one as
|
|
175
|
+
**lent** rows, `lent{to, within, until}`, in whichever store keeps the group.
|
|
176
|
+
`hold(store, group, person, allowed)` holds the person's sandbox awake for
|
|
177
|
+
`HELD` seconds, the longest command and then the nap, unless `allowed` others
|
|
178
|
+
already are; it answers how many and writes nothing then. It rewrites the row
|
|
179
|
+
only once half the hold is spent. `slept` gives the place back when the host
|
|
180
|
+
stops the container after preserving its files. `lending(store, group)` is every
|
|
181
|
+
sandbox the group has lent, asleep ones included, so the host can preserve their
|
|
182
|
+
files when a group is trashed. A row stays discoverable with its group in the
|
|
183
|
+
trash.
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { equal } from '@yaks/testing'
|
|
187
|
+
import { graph } from '@yaks/graph'
|
|
188
|
+
import { ram } from '@yaks/ram'
|
|
189
|
+
import { loadVocab } from '@yaks/vocab'
|
|
190
|
+
import { kernelDoc } from '@yaks/kernel'
|
|
191
|
+
import { sandboxDoc } from '@yaks/sandbox'
|
|
192
|
+
import { hold, lending } from '@yaks/sandbox/cloudflare'
|
|
193
|
+
|
|
194
|
+
let vocab = loadVocab([kernelDoc, sandboxDoc])
|
|
195
|
+
let store = graph({ storage: ram(vocab), vocab })
|
|
196
|
+
let [space, ada, bob] = ['a', 'b', 'c'].map((n) =>
|
|
197
|
+
`a0000000-0000-4000-8000-00000000000${n}`
|
|
198
|
+
)
|
|
199
|
+
await store.apply(
|
|
200
|
+
[space, ada, bob].map((eid) => ({ entity: { eid }, opened: {} })),
|
|
201
|
+
)
|
|
202
|
+
equal(await hold(store, space, ada, 1), { held: true })
|
|
203
|
+
equal(await hold(store, space, bob, 1), { held: false, awake: 1 })
|
|
204
|
+
equal((await lending(store, space)).map((l) => l.to), [ada])
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Bubblewrap sandboxes
|
|
208
|
+
|
|
209
|
+
`sandboxProvider` lends a persistent `/workspace`, backed by one directory per
|
|
210
|
+
sandbox under its configured `dir`. Commands see that workspace, read-only
|
|
211
|
+
`/usr`, `/bin`, `/lib` and `/lib64`, a private `/tmp`, minimal `/dev`, and their
|
|
212
|
+
own PID, IPC, UTS, user, network and cgroup namespaces. They do not see the
|
|
213
|
+
host's home, `/etc`, other workspaces, the control socket or the user manager.
|
|
214
|
+
Files read and written through Workspace use the same restricted filesystem;
|
|
215
|
+
absolute paths are guest paths, not host paths. Export resolves symlinks in that
|
|
216
|
+
filesystem, so a symlink cannot expose a host file.
|
|
217
|
+
|
|
218
|
+
The provider requires Linux, `/usr/bin/bwrap`, `/usr/bin/python3`, bash, a
|
|
219
|
+
`flock`, a systemd user manager, usable unprivileged namespaces and delegated
|
|
220
|
+
cgroup v2 CPU, memory and pids controllers. It refuses to lend a sandbox if
|
|
221
|
+
bubblewrap cannot execute or the scope's limits are absent. It never falls back
|
|
222
|
+
to the unrestricted process provider. No Docker daemon, image, or installation
|
|
223
|
+
by this package is involved.
|
|
224
|
+
|
|
225
|
+
The host supplies these **sandbox limits** for the whole sandbox:
|
|
226
|
+
|
|
227
|
+
| Provider option | Meaning |
|
|
228
|
+
| ------------------ | ---------------------------------------------------------------------- |
|
|
229
|
+
| `limits.cpuQuota` | Percentage of one CPU, mapped to `CPUQuota`; 50 is half one CPU |
|
|
230
|
+
| `limits.memoryMax` | Maximum resident bytes, mapped to `MemoryMax`; swap is disabled |
|
|
231
|
+
| `limits.tasksMax` | Maximum processes and threads, mapped to `TasksMax` |
|
|
232
|
+
| `binds` | More host paths, each bound read-only at the same guest path |
|
|
233
|
+
| `layers` | What the host made of the commit, each bound read-only at a guest path |
|
|
234
|
+
|
|
235
|
+
All commands and the detached supervisor share one systemd scope. Starting
|
|
236
|
+
another command does not grant another quota. CPU quota must be finite and at
|
|
237
|
+
least 1; memory must be an integer of at least 16 MiB; tasks must be an integer
|
|
238
|
+
of at least 16. The host chooses workable limits for its workload. An OOM kills
|
|
239
|
+
the scope; `connect` replaces its supervisor, keeps the workspace and marks
|
|
240
|
+
unfinished receipts with an unknown exit code. Connecting with different limits
|
|
241
|
+
or network policy is refused rather than silently changing a running sandbox.
|
|
242
|
+
|
|
243
|
+
This example requires the Linux namespace and systemd configuration above:
|
|
244
|
+
|
|
245
|
+
```ts ignore
|
|
246
|
+
import { equal } from '@yaks/testing'
|
|
247
|
+
import { sandboxProvider } from '@yaks/sandbox/bubblewrap'
|
|
248
|
+
|
|
249
|
+
let dir = await Deno.makeTempDir({ prefix: 'sb-' })
|
|
250
|
+
let options = {
|
|
251
|
+
dir,
|
|
252
|
+
limits: { cpuQuota: 50, memoryMax: 256 * 1024 * 1024, tasksMax: 64 },
|
|
253
|
+
}
|
|
254
|
+
let provider = sandboxProvider(options)
|
|
255
|
+
let ref = { id: 'example' }
|
|
256
|
+
try {
|
|
257
|
+
let { workspace: machine } = await provider.request!(ref)
|
|
258
|
+
await machine.write('hello.txt', 'hello')
|
|
259
|
+
equal(await machine.read('/workspace/hello.txt'), 'hello')
|
|
260
|
+
let command = await machine.start('sleep 30', undefined, 'durable-call')
|
|
261
|
+
let reopened = await sandboxProvider(options).connect(ref)
|
|
262
|
+
equal(await reopened.workspace.receipt!('durable-call'), command)
|
|
263
|
+
await reopened.workspace.kill(command, 'SIGKILL')
|
|
264
|
+
} finally {
|
|
265
|
+
await provider.dispose(ref)
|
|
266
|
+
await Deno.remove(dir, { recursive: true })
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
The example runs against the actual namespaces and user manager, not a fake
|
|
271
|
+
launcher. The provider's root must be short enough for its Unix socket path (107
|
|
272
|
+
bytes including the sandbox id and `/control/control.sock`). Control files are
|
|
273
|
+
private to the host and separate from the workspace. Their receipts are local
|
|
274
|
+
backend state, not graph rows or shell-visible files. A filesystem lock
|
|
275
|
+
serializes lifecycle operations across hosts; command receipt creation is
|
|
276
|
+
serialized by the supervisor. A durable call is never launched again, even when
|
|
277
|
+
its launch was interrupted and its outcome is unknown. Output and exit status
|
|
278
|
+
survive the requesting host's exit; `look`, `tail` and `kill` reach the same
|
|
279
|
+
supervisor from a replacement host. Termination targets the command's process
|
|
280
|
+
group and PID namespace; dispose stops the entire scope while retaining the
|
|
281
|
+
workspace and receipts. Restore it before exporting files.
|
|
282
|
+
|
|
283
|
+
`request` optionally calls idempotent `prepare(from, machine, '/workspace')` to
|
|
284
|
+
populate a graph commit. A failed preparation is retried by `request`; `connect`
|
|
285
|
+
refuses incomplete preparation. Preparation is recorded outside the guest
|
|
286
|
+
workspace and reused on retry. `image` and existing-machine attachment are
|
|
287
|
+
unsupported: use the process provider to explicitly attach local disk.
|
|
288
|
+
|
|
289
|
+
Network is isolated by default. `network: true` explicitly shares the host's
|
|
290
|
+
network namespace, including access to loopback services, and therefore weakens
|
|
291
|
+
isolation. DNS configuration and extra tools are not inherited; a host can
|
|
292
|
+
provision what its workload needs through the workspace. `env(session?)`
|
|
293
|
+
supplies only explicit command environment and short-lived graph grants; nothing
|
|
294
|
+
in the launching host's environment is inherited by commands. This provider
|
|
295
|
+
constructs a namespace sandbox, not a microVM: it shares the host kernel and
|
|
296
|
+
does not claim protection against kernel exploits.
|
|
297
|
+
|
|
298
|
+
## PVE LXC clones
|
|
299
|
+
|
|
300
|
+
The `./pve` adapter clones an explicitly configured template on the computer
|
|
301
|
+
reached by its transport.
|
|
302
|
+
`pveClones(graph, { transport, state, template,
|
|
303
|
+
storage, vmidRange })` requires
|
|
304
|
+
a storage id and reserved VMID range; a stable hash of the sandbox eid chooses
|
|
305
|
+
one VMID. A collision refuses the request, rather than adopting another
|
|
306
|
+
container or trying an unreserved VMID. A random clone description token and
|
|
307
|
+
host receipts establish ownership. Keep `state` after disposal, as with Docker.
|
|
308
|
+
A stopped clone can be started by `request`; `connect` refuses stopped, missing
|
|
309
|
+
or incomplete clones.
|
|
310
|
+
|
|
311
|
+
This example requires a PVE computer, a prepared template, a reserved pool and
|
|
312
|
+
permission to clone into the specified storage:
|
|
313
|
+
|
|
314
|
+
```ts ignore
|
|
315
|
+
import { pveClones } from '@yaks/sandbox/pve'
|
|
316
|
+
|
|
317
|
+
let provider = pveClones(graph, {
|
|
318
|
+
transport: pveTransport,
|
|
319
|
+
state: '/var/lib/yak-sandboxes',
|
|
320
|
+
template: 9000,
|
|
321
|
+
storage: 'guests',
|
|
322
|
+
vmidRange: [9200, 9299],
|
|
323
|
+
})
|
|
324
|
+
let box = await provider.request!({ id: sandboxEid })
|
|
325
|
+
await box.workspace.write('result.txt', 'ready')
|
|
326
|
+
await provider.dispose({ id: sandboxEid })
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
The template needs bash, setsid, flock, base64, Python 3 and Linux `/proc`.
|
|
330
|
+
Commands use `pct exec` through the PVE computer; no QEMU guest-agent API is
|
|
331
|
+
assumed for LXC. `image` is refused; `from` requires an idempotent `prepare`
|
|
332
|
+
callback. Disposal stops and retains the token-matching clone in trash. No PVE
|
|
333
|
+
account, API token, template or storage allocation is created by the adapter.
|
|
334
|
+
|
|
335
|
+
## Bubblewrap and Cloudflare
|
|
336
|
+
|
|
337
|
+
The `./bubblewrap` adapter provides an explicitly local Linux jail, with CPU,
|
|
338
|
+
memory and task limits. It requires user namespaces, bubblewrap and delegated
|
|
339
|
+
cgroup controls. See its `SandboxProviderOpts` for filesystem and network
|
|
340
|
+
choices. It retains the workspace and a trash receipt outside the jail
|
|
341
|
+
directory. It is not a fallback for an unreachable remote computer.
|
|
342
|
+
|
|
343
|
+
The `./cloudflare` adapter binds a deployed sandbox namespace. Its image comes
|
|
344
|
+
from the deployment: `from`, `image` and foreign addresses are refused. Computer
|
|
345
|
+
placement remains unknown when the backend cannot disclose it. Cloudflare
|
|
346
|
+
requires a durable, serialized lifecycle seam to distinguish a requested
|
|
347
|
+
resource from missing or disposed writable storage. Connecting does not silently
|
|
348
|
+
rebuild that storage, unless the host says it is ephemeral. The host owns
|
|
349
|
+
billing, grants and the store its lent rows are kept in. Cloudflare SDK process
|
|
350
|
+
ids are recovered through that namespace, and commands retain the SDK timeout.
|
|
351
|
+
Unlike the other adapters, absolute export names are accepted for the published
|
|
352
|
+
sandbox ship interface.
|
|
353
|
+
|
|
354
|
+
## Configure graph provisioning
|
|
355
|
+
|
|
356
|
+
The plugin's `with.providers` binds provider names to implementation modules and
|
|
357
|
+
computer eids. Docker and PVE have built-in JSON bindings; other modules export
|
|
358
|
+
`sandboxProvider(host, options, placement)`. Module factories can supply
|
|
359
|
+
Cloudflare SDK bindings or bubblewrap's local jail setup. `placement` carries
|
|
360
|
+
the explicit computer eid and its transport, when known. Programmatic hosts may
|
|
361
|
+
pass `{ provider, machine }` directly. No default provider or computer is
|
|
362
|
+
guessed.
|
|
363
|
+
|
|
364
|
+
```json
|
|
365
|
+
{
|
|
366
|
+
"use": "@yaks/sandbox",
|
|
367
|
+
"with": {
|
|
368
|
+
"providers": {
|
|
369
|
+
"coder": {
|
|
370
|
+
"use": "@yaks/sandbox/docker",
|
|
371
|
+
"machine": "775c579e-fdaa-4548-838a-89838ef83879",
|
|
372
|
+
"with": {
|
|
373
|
+
"state": "/home/coder/.local/state/yak-sandboxes",
|
|
374
|
+
"identity": "/home/graph/.ssh/coder"
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
The computer row's `machine.address` selects `local://` or `ssh://user@host`.
|
|
383
|
+
`identity` and `knownHosts` are optional SSH file paths, not credentials stored
|
|
384
|
+
in graph rows. The binding's `state` path is interpreted on that computer. PVE
|
|
385
|
+
additionally requires `template`, `storage` and a two-number `vmidRange`; linked
|
|
386
|
+
clones require every template volume to use that storage. Built-in JSON bindings
|
|
387
|
+
do not invent Git preparation; a module factory supplies an idempotent `prepare`
|
|
388
|
+
callback when creating from a graph commit.
|
|
389
|
+
|
|
390
|
+
`sandbox_new` records an intention and returns its sandbox/workspace row. The
|
|
391
|
+
`sandbox_provision` effect creates the resource later; read `sandbox.status`
|
|
392
|
+
until it is ready or failed. `sandbox_dispose` stops execution and retains
|
|
393
|
+
editable files in trash; `sandbox_restore` restores the same resource. Neither
|
|
394
|
+
purges storage. `sandbox_dispose` is an explicit durable tool call, not a
|
|
395
|
+
home-switch action. Adapter exports and these tools do not change which plugins
|
|
396
|
+
the host enables or grant access to another computer.
|
|
397
|
+
|
|
398
|
+
Disposed Docker sandboxes and PVE clones keep their reserved backend identifiers
|
|
399
|
+
while in the trash. Bubblewrap keeps the complete workspace and control files.
|
|
400
|
+
`provider.restore(ref)` returns the retained resource; a graph-managed sandbox
|
|
401
|
+
uses `sandbox_restore`. These adapters have no automatic purge.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Workspace } from "@yaks/machine";
|
|
2
|
+
import type { SandboxProvider, SandboxRef } from "./provider.js";
|
|
3
|
+
/** Limits for the whole sandbox, including its supervisor and all commands.
|
|
4
|
+
* cpuQuota is a percentage of one CPU; memoryMax is bytes; tasksMax counts
|
|
5
|
+
* processes and threads. No limit may be left unbounded. */ export type SandboxLimits = {
|
|
6
|
+
cpuQuota: number;
|
|
7
|
+
memoryMax: number;
|
|
8
|
+
tasksMax: number;
|
|
9
|
+
};
|
|
10
|
+
/** A host tree a sandbox reads and never writes, bound read-only at `target`
|
|
11
|
+
* in the guest (inside /workspace, say). */ export type Layer = {
|
|
12
|
+
source: string;
|
|
13
|
+
target: string;
|
|
14
|
+
};
|
|
15
|
+
/** Provider data plus the host's optional commit preparation and grants.
|
|
16
|
+
* The default filesystem is the host's read-only /usr, /bin, /lib and /lib64,
|
|
17
|
+
* the read-only `binds`, and the `layers` the host made of the commit.
|
|
18
|
+
* Network access is opt-in; no host home, /etc, runtime sockets or environment
|
|
19
|
+
* is inherited. image is refused: this provider does not boot images. */ export type SandboxProviderOpts = {
|
|
20
|
+
dir: string;
|
|
21
|
+
/** Existing private directory for released sandboxes, on bulk storage. */ archive?: string;
|
|
22
|
+
limits: SandboxLimits;
|
|
23
|
+
network?: boolean;
|
|
24
|
+
/** More host paths commands read, each bound read-only at the same path
|
|
25
|
+
* (a toolchain's prefix, its download cache) */ binds?: string[];
|
|
26
|
+
/** What the host made of commit `from` that the sandbox shares rather than
|
|
27
|
+
* copies (a dependency tree, say), asked once as the sandbox is made, before
|
|
28
|
+
* `prepare` */ layers?: (from: string) => Promise<Layer[]>;
|
|
29
|
+
env?: (session?: string) => Record<string, string>;
|
|
30
|
+
prepare?: (from: string, workspace: Workspace, cwd: string) => Promise<void>;
|
|
31
|
+
birth?: number;
|
|
32
|
+
};
|
|
33
|
+
/** Local isolated sandboxes; every command in a sandbox shares its limits.
|
|
34
|
+
* Reconstruct this provider with the same data to connect sandboxes or recover
|
|
35
|
+
* command receipts. Release stops the whole scope and retains its files until restored. */ export declare let sandboxProvider: (o: SandboxProviderOpts) => SandboxProvider & {
|
|
36
|
+
archiveReleased: (ref: SandboxRef) => Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=bubblewrap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bubblewrap.d.ts","sources":["../bubblewrap.ts"],"names":[],"mappings":"AAUA,cAA0B,SAAS,6BAAkC;AACrE,cAEE,eAAe,EACf,UAAU,wBACU;AAKtB;;0DAE0D,GAC1D,YAAY;EACV,UAAU,MAAM;EAChB,WAAW,MAAM;EACjB,UAAU,MAAM;;AAGlB;0CAC0C,GAC1C,YAAY;EAAU,QAAQ,MAAM;EAAE,QAAQ,MAAM;;AAEpD;;;;uEAIuE,GACvE,YAAY;EACV,KAAK,MAAM;EACX,wEAAwE,GACxE,UAAU,MAAM;EAChB,QAAQ;EACR,UAAU,OAAO;EACjB;gDAC8C,GAC9C,QAAQ,MAAM;EACd;;eAEa,GACb,UAAU,MAAM,MAAM,KAAK,QAAQ;EACnC,OAAO,UAAU,MAAM,KAAK,OAAO,MAAM,EAAE,MAAM;EACjD,WAAW,MAAM,MAAM,EAAE,WAAW,WAAW,KAAK,MAAM,KAAK,QAAQ,IAAI;EAC3E,QAAQ,MAAM;;AAiChB;;yFAEyF,GACzF,OAAO,YAAI,kBACT,GAAG,wBACF;EACD,kBAAkB,KAAK,eAAe,QAAQ,IAAI;EA8cnD"}
|