kosa8 0.3.0 → 0.4.0
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.
- checksums.yaml +4 -4
- data/README.md +152 -34
- data/lib/kosa8/cloud.rb +362 -21
- data/lib/kosa8/cloud_features.rb +659 -0
- data/lib/kosa8/errors.rb +41 -3
- data/lib/kosa8/version.rb +1 -1
- data/lib/kosa8.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9752d0bec0e95e842ce3de9687c7cc1a13da70cf896910c44a549da39b9bbd16
|
|
4
|
+
data.tar.gz: a5bd9e55f88bfc3fdd439a3d23c8f797495dbd84c27a1d61f2f5d1ab48e48c78
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2893aac3ff20089bd6fe2bce85d3f373eb19a5f15449d572e74a5dc020e3566018b058842a850582366a0e3f3538c5e72dc72d277a7e198c99225a20c0e13f77
|
|
7
|
+
data.tar.gz: f71b423a14d7fd665087ba147c47bb706eee278cea20e400258f92150e036ae2fbbbc3fb819504909eb5ecdcbebb35ee11acdcd381bf9490eebfbf38fe164ce7
|
data/README.md
CHANGED
|
@@ -3,6 +3,155 @@
|
|
|
3
3
|
The Ruby SDK for [kosa8](https://kosa8.com): microVM sandboxes for AI agents. No dependencies; Ruby 2.6+.
|
|
4
4
|
Method for method it matches [`@kosa8/sdk`](https://github.com/kosa8ai/kosa8-js), the Python `kosa8` package and `kosa8-go`.
|
|
5
5
|
|
|
6
|
+
## Quickstart: kosa8 Cloud
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
gem install kosa8 -v 0.4.0
|
|
10
|
+
# or in a Gemfile: gem "kosa8", "~> 0.4.0"
|
|
11
|
+
export KOSA8_API_KEY=k8c_live_... # made in the cloud dashboard
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
require "kosa8"
|
|
16
|
+
|
|
17
|
+
cloud = Kosa8::Cloud.new # KOSA8_API_KEY from the environment; or Kosa8::Cloud.new(api_key: "k8c_live_...")
|
|
18
|
+
|
|
19
|
+
box = cloud.sandboxes.create(template: "python", ttl_s: 3600) # deleted automatically after an hour
|
|
20
|
+
r = box.exec(["python3", "-c", "print(6 * 7)"])
|
|
21
|
+
puts r.exit_code, r.stdout # 0, 42
|
|
22
|
+
|
|
23
|
+
box.exec("echo hello > /workspace/hello.txt") # only /workspace persists between commands
|
|
24
|
+
puts box.exec("cat /workspace/hello.txt").stdout
|
|
25
|
+
|
|
26
|
+
box.files.write("/workspace/app.py", "print('hi')\n") # needs the host's data plane (see below)
|
|
27
|
+
puts box.files.read_text("/workspace/app.py")
|
|
28
|
+
|
|
29
|
+
box.sleep # costs only snapshot storage while asleep
|
|
30
|
+
box.exec("ls /workspace") # wakes it, files and all
|
|
31
|
+
box.delete
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`cloud.templates` lists the templates your org can start. Without `template:` you get the org's
|
|
35
|
+
default, `base` (see below).
|
|
36
|
+
|
|
37
|
+
### More of the Cloud API
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
box.update(ttl_s: 7200) # PATCH: a new lifetime from now
|
|
41
|
+
box.update(clear_lifetime: true) # no automatic delete (sends ttl_s: null)
|
|
42
|
+
box.update(sleep_after_s: 0) # never sleep when idle
|
|
43
|
+
|
|
44
|
+
cloud.sandboxes.list # an Array, as before; list(all: true) includes deleted ones
|
|
45
|
+
cloud.sandboxes.each(all: true) { |b| puts "#{b.id} #{b.state} #{b.info["delete_after"]}" } # every page
|
|
46
|
+
page = cloud.sandboxes.list_page(limit: 50) # one page: page.sandboxes, page.next_cursor (nil at the end)
|
|
47
|
+
|
|
48
|
+
# These need the host's data plane. On a host without it they raise
|
|
49
|
+
# Kosa8::UnsupportedError (501 not_available / not_supported): not every
|
|
50
|
+
# kosa8 Cloud host has it yet.
|
|
51
|
+
box.files.write("/workspace/data.bin", File.binread("data.bin")) # bytes or text, at most 8 MiB
|
|
52
|
+
bytes = box.files.read("/workspace/data.bin") # a binary String
|
|
53
|
+
puts box.files.list("/workspace").map { |f| f["name"] }
|
|
54
|
+
box.files.mkdir("/workspace/out")
|
|
55
|
+
box.files.delete("/workspace/out", recursive: true)
|
|
56
|
+
|
|
57
|
+
ckpt = box.checkpoint("before the risky bit") # memory, processes and disk
|
|
58
|
+
box.exec("rm -rf /workspace/*")
|
|
59
|
+
r = box.rewind(ckpt["id"]) # back; r.backup is the automatic checkpoint of the state before
|
|
60
|
+
box.checkpoints # oldest first
|
|
61
|
+
forks = box.fork(4) # four live copies, CloudSandbox objects
|
|
62
|
+
p box.ports # listening TCP ports
|
|
63
|
+
|
|
64
|
+
cloud.functions.deploy("hello", {
|
|
65
|
+
"runtime" => "python3.12",
|
|
66
|
+
"code" => "def handler(event):\n return {\"hello\": event.get(\"name\", \"world\")}\n"
|
|
67
|
+
})
|
|
68
|
+
out = cloud.functions.invoke("hello", { "name" => "kosa8" })
|
|
69
|
+
p out["ok"], out["result"] # true, {"hello"=>"kosa8"}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`cloud.me` and `cloud.usage` show the org and this month's bill estimate. The address is
|
|
73
|
+
`KOSA8_CLOUD_URL` (default `https://api.kosa8.com`, https only except localhost).
|
|
74
|
+
|
|
75
|
+
### Sized and Docker sandboxes, env, snapshots
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
box = cloud.sandboxes.create(size: "4x8", image: "kosa8-template/universal-24.04-20261115.2",
|
|
79
|
+
docker: true, env: { "NODE_ENV" => "production" }, name: "web",
|
|
80
|
+
labels: { "team" => "a" }, ttl_s: 3600)
|
|
81
|
+
# a sized create boots cold; create waits until it runs (wait: false to skip)
|
|
82
|
+
|
|
83
|
+
snap = box.snapshot("warm") # durable: kept until deleted
|
|
84
|
+
copy = cloud.sandboxes.create(snapshot: snap["id"])
|
|
85
|
+
cloud.snapshots.delete(snap["id"]) # also list, get
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Also `cpus:`/`mem_mb:`, `disk_gb:`, `persistent:`, `cpu: "dedicated"`, `region:` (`us`, `eu` or
|
|
89
|
+
an id), `secrets:` and `egress:`. `list` takes `name:` and `labels:`.
|
|
90
|
+
|
|
91
|
+
### Background commands, previews, terminal and more
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
job = box.exec_background("npm run build") # no 30 s limit
|
|
95
|
+
job = box.wait_background(job) # or background_exec(id) to poll once
|
|
96
|
+
puts job["exit"], job["output"]
|
|
97
|
+
|
|
98
|
+
puts box.previews.create(3000)["url"] # private signed URL; public: true for none
|
|
99
|
+
box.terminal(cols: 120, rows: 40)["url"] # a wss:// PTY URL
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Files: `files.download(path)` / `files.upload(path, data)` (no 8 MiB cap), `files.link(path,
|
|
103
|
+
method: "PUT")` (presigned URL).
|
|
104
|
+
- `egress`, `set_egress(presets: ["pypi"])`, `reset_egress`; `cloud.egress_presets`.
|
|
105
|
+
- `run_code("print(6*7)", context: "a")` (stateful Python; persistent sandbox with python3),
|
|
106
|
+
`code_contexts`, `create_code_context`, `reset_code_context`, `delete_code_context`.
|
|
107
|
+
- `schedules.create(cron: "0 * * * *", cmd: "date")`, `list`, `run`, `delete`.
|
|
108
|
+
- `metrics`, `logs(limit:)`, `set_labels`, `secrets`; `cloud.capabilities`.
|
|
109
|
+
|
|
110
|
+
### Secrets and Pipelines
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
# Read the value from a file or $stdin, never from argv or code.
|
|
114
|
+
cloud.secrets.set("npm-token", value: $stdin.read.strip, upstream: "https://registry.npmjs.org",
|
|
115
|
+
pipelines: true) # also list, delete
|
|
116
|
+
|
|
117
|
+
run = cloud.pipelines.run(repo_url: "https://github.com/me/app", ref: "main", secrets: ["npm-token"])
|
|
118
|
+
run = cloud.pipelines.wait(run["id"])
|
|
119
|
+
cloud.pipelines.logs(run["id"])["logs"].each { |l| puts l["text"] }
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Also `runs`, `get`, `cancel`, `artifacts`, `download_artifact`, `create_hook` (webhook URL and
|
|
123
|
+
optional `cron:`), `hooks`, `delete_hook`.
|
|
124
|
+
|
|
125
|
+
### The base template
|
|
126
|
+
|
|
127
|
+
- The `base` template is **Alpine Linux**: `sh` is busybox, and there is no `bash`, `python` or `git`.
|
|
128
|
+
Use `template: "python"` (or another key from `cloud.templates`) for Python, or pass `image:` to `exec`.
|
|
129
|
+
- **Each `exec` runs in a fresh container, and only `/workspace` persists.** `cd`, `export` and anything
|
|
130
|
+
installed outside `/workspace` (`apk add`, a system `pip install`) are gone by the next command. Install
|
|
131
|
+
into `/workspace` (for example `pip install --target /workspace/lib ...` with `PYTHONPATH=/workspace/lib`),
|
|
132
|
+
or chain the steps in one `sh -c`.
|
|
133
|
+
- **A buffered `exec` is cut off after about 30 seconds** and raises `Kosa8::ExecTimeoutError`. Run long
|
|
134
|
+
work in the background and poll it:
|
|
135
|
+
`box.exec("nohup sh -c 'make test > /workspace/test.log 2>&1; echo $? > /workspace/test.exit' &")`.
|
|
136
|
+
- A request body (command and env) is at most 64 KiB; a file read or write is at most 8 MiB.
|
|
137
|
+
- Set `ttl_s:` (60 s to 90 days) or `delete_after:` (Unix ms) on create so a crashed script cannot leave a
|
|
138
|
+
sandbox running. Bad values raise `ArgumentError` before any request.
|
|
139
|
+
- The Cloud API has no sandbox labels or metadata.
|
|
140
|
+
|
|
141
|
+
### Cloud errors
|
|
142
|
+
|
|
143
|
+
| Error | When | What to do |
|
|
144
|
+
|---|---|---|
|
|
145
|
+
| `Kosa8::PaymentRequiredError` (402) | no payment method (`payment_required`), spending cap reached (`spending_cap_reached`), Store price not accepted (`price_not_accepted`) | show `e.upgrade_url`; an agent should relay `e.agent_message` to its person and stop |
|
|
146
|
+
| `Kosa8::OrgFrozenError` (423, `org_frozen`) | the org is frozen in the Security Center | an owner or admin unfreezes it; retrying does not help |
|
|
147
|
+
| `Kosa8::RateLimitError` (429) | too many requests, or `quota_exceeded` (active sandbox limit) | wait `e.retry_after` seconds when set, or delete sandboxes |
|
|
148
|
+
| `Kosa8::ExecTimeoutError` (504 `exec_timeout`, or today's 502 "timed out" on exec) | a buffered exec ran past about 30 s | run it in the background and poll |
|
|
149
|
+
| `Kosa8::UnsupportedError` (501) | the host has no data plane yet (files, checkpoints, rewind, fork, ports) | use `exec`, or try later |
|
|
150
|
+
|
|
151
|
+
Every Cloud error is a `Kosa8::ApiError` carrying the server's stable `e.code`, its message and `e.body`.
|
|
152
|
+
|
|
153
|
+
## Local daemon
|
|
154
|
+
|
|
6
155
|
```ruby
|
|
7
156
|
require "kosa8"
|
|
8
157
|
|
|
@@ -42,11 +191,11 @@ Where requests go, the same as the CLI:
|
|
|
42
191
|
On Windows the engine runs in WSL2: run `kosa8 wsl setup` once and `Kosa8::Client.new` finds it.
|
|
43
192
|
`client.transport.endpoint.target` shows where requests go.
|
|
44
193
|
|
|
45
|
-
## Errors
|
|
194
|
+
## Errors (local daemon)
|
|
46
195
|
|
|
47
196
|
All errors inherit `Kosa8::Error` and carry the daemon's message and remediation:
|
|
48
197
|
`ConnectionError`, `BadRequestError`, `AuthError`, `PolicyError` › `ApprovalRequiredError` (`#approval`),
|
|
49
|
-
`NotFoundError`, `ConflictError`, `UnsupportedError`, `PaymentRequiredError` (402), `RateLimitError` (429), `StreamError` (`#stdout`/`#stderr` so far),
|
|
198
|
+
`NotFoundError`, `ConflictError`, `UnsupportedError`, `PaymentRequiredError` (402), `OrgFrozenError` (423), `RateLimitError` (429), `ExecTimeoutError`, `StreamError` (`#stdout`/`#stderr` so far),
|
|
50
199
|
`CheckpointError`, `TimeoutError`. Methods that a policy may gate take `approval:`.
|
|
51
200
|
|
|
52
201
|
## API
|
|
@@ -97,44 +246,13 @@ k.list_templates; k.delete_template("py-data")
|
|
|
97
246
|
A non-zero exit or a failed git op is a result (`ok?` false), not an exception; a failed template build
|
|
98
247
|
raises `Kosa8::StreamError`.
|
|
99
248
|
|
|
100
|
-
## kosa8 Cloud
|
|
101
|
-
|
|
102
|
-
The same sandboxes on kosa8 Cloud instead of the local daemon. Make an API
|
|
103
|
-
key in the cloud dashboard and set `KOSA8_API_KEY`.
|
|
104
|
-
|
|
105
|
-
```ruby
|
|
106
|
-
require "kosa8"
|
|
107
|
-
|
|
108
|
-
cloud = Kosa8::Cloud.new # or Kosa8::Cloud.new(api_key: "k8c_live_...")
|
|
109
|
-
|
|
110
|
-
box = cloud.sandboxes.create(template: "python", sleep_after_s: 600)
|
|
111
|
-
r = box.exec(["python3", "-c", "print(6 * 7)"])
|
|
112
|
-
puts r.exit_code, r.stdout # 0, 42
|
|
113
|
-
box.sleep # costs only snapshot storage while asleep
|
|
114
|
-
box.exec("ls /workspace") # wakes it, files and all
|
|
115
|
-
box.delete
|
|
116
|
-
|
|
117
|
-
cloud.functions.deploy("hello", {
|
|
118
|
-
"runtime" => "python3.12",
|
|
119
|
-
"code" => "def handler(event):\n return {\"hello\": event.get(\"name\", \"world\")}\n"
|
|
120
|
-
})
|
|
121
|
-
out = cloud.functions.invoke("hello", { "name" => "kosa8" })
|
|
122
|
-
p out["ok"], out["result"] # true, {"hello"=>"kosa8"}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
A new organization runs nothing until a payment method is on file; until
|
|
126
|
-
then creating a sandbox raises `Kosa8::PaymentRequiredError` (`code`
|
|
127
|
-
`payment_required`), whose message says what to do. Every error from the
|
|
128
|
-
cloud carries its stable `code`. The address is `KOSA8_CLOUD_URL` (default
|
|
129
|
-
`https://api.kosa8.cloud`, https only except localhost); `cloud.me` and
|
|
130
|
-
`cloud.usage` show the org and this month's bill estimate.
|
|
131
|
-
|
|
132
249
|
## Development
|
|
133
250
|
|
|
134
251
|
```sh
|
|
135
252
|
ruby -w test/test_client.rb # mock daemon on a unix socket
|
|
136
253
|
ruby -w test/test_toolbox.rb
|
|
137
254
|
ruby -w test/test_cloud.rb # stub transport and a local socket
|
|
255
|
+
ruby -w test/test_cloud_parity.rb # Cloud API parity, stub transport
|
|
138
256
|
ruby -w -Ilib -Itest -e 'Dir["test/test_*.rb"].sort.each { |f| require File.expand_path(f) }' # all, as CI does
|
|
139
257
|
ruby examples/smoke.rb # live, against a running `kosa8 daemon`
|
|
140
258
|
```
|