kosa8 0.5.2 → 0.5.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e865379c864f1e968325212f3e06cb82056a58357aa06d0f443a651333d7928
4
- data.tar.gz: 340f70bfbb6ef787f0f506119462d2bf5b785a5eb546f21d645b54d7caf66621
3
+ metadata.gz: 2f844e8a5a44b134b65d86679d015a5a2e631239d938df854d745b393a2b99bb
4
+ data.tar.gz: a3b44827f4db786bc9c798c46f13fbfd1aa7c6e593abcda6362bb1900da8d65f
5
5
  SHA512:
6
- metadata.gz: d8aa6298bb875c0914621763de073c5326811c7187928df33425f379a31561bc3078fee0366f91d0133dde7f787ac8e0672679f09d4c8f51f0c18388edf506a5
7
- data.tar.gz: bc7c0768719881ea2833e4ff81323a1b5bd4a7cf3fa5af1d268bb597dd377add42835445d80420354858ee5f7c38ae6bc4a5d620522d0528cd3805a4d86f7993
6
+ metadata.gz: 3e661816f8189fa290867c4a3d039a2a8250d6a07083797f5c2202838e3a4b11c843e6a86d31af06ec8871d2dfc1be6e931e3cf4d2e1eac165ebbf6885264014
7
+ data.tar.gz: 550fa6e4c577c0482ec897842ea7334a34429784ebbaafc831e4f0758d75a557a28df71cb4f2f5e6ccc2950f6bf679122f75c33c0c3d33578fdfad73cd6a3c56
data/README.md CHANGED
@@ -6,8 +6,8 @@ Method for method it matches [`@kosa8/sdk`](https://github.com/kosa8ai/kosa8-js)
6
6
  ## Quickstart: kosa8 Cloud
7
7
 
8
8
  ```sh
9
- gem install kosa8 -v 0.4.0
10
- # or in a Gemfile: gem "kosa8", "~> 0.5.0"
9
+ gem install kosa8
10
+ # or in a Gemfile: gem "kosa8", "~> 0.5"
11
11
  export KOSA8_API_KEY=k8c_live_... # made in the cloud dashboard
12
12
  ```
13
13
 
@@ -20,7 +20,7 @@ box = cloud.sandboxes.create(template: "python", ttl_s: 3600) # deleted automat
20
20
  r = box.exec(["python3", "-c", "print(6 * 7)"])
21
21
  puts r.exit_code, r.stdout # 0, 42
22
22
 
23
- box.exec("echo hello > /workspace/hello.txt") # only /workspace persists between commands
23
+ box.exec("echo hello > /workspace/hello.txt") # /workspace is kept between commands
24
24
  puts box.exec("cat /workspace/hello.txt").stdout
25
25
 
26
26
  box.files.write("/workspace/app.py", "print('hi')\n") # needs the host's data plane (see below)
@@ -167,11 +167,15 @@ Kosa8::Cloud.new(api_key: tok["secret"]) # hand this to your end user, not your
167
167
 
168
168
  - The `base` template is **Alpine Linux**: `sh` is busybox, and there is no `bash`, `python` or `git`.
169
169
  Use `template: "python"` (or another key from `cloud.templates`) for Python, or pass `image:` to `exec`.
170
- - **Each `exec` runs in a fresh container, and only `/workspace` persists.** `cd`, `export` and anything
171
- installed outside `/workspace` (`apk add`, a system `pip install`) are gone by the next command. Install
172
- into `/workspace` (for example `pip install --target /workspace/lib ...` with `PYTHONPATH=/workspace/lib`),
173
- or chain the steps in one `sh -c`.
174
- - **A buffered `exec` runs at most 90 seconds** and raises `Kosa8::ExecTimeoutError`. Run long
170
+ - **Only `/workspace` is sure to persist between commands.** A command may run in a fresh container, so
171
+ `cd`, `export`, processes it leaves running and anything installed outside `/workspace` (`apk add`, a
172
+ system `pip install`) may be gone by the next command. Install into `/workspace` (for example
173
+ `pip install --target /workspace/lib ...` with `PYTHONPATH=/workspace/lib`), chain the steps in one
174
+ `sh -c`, or create with `persistent: true` (one long-lived container) to keep the whole filesystem and
175
+ background processes; the code interpreter (`run_code`) needs it, and it counts against your plan's
176
+ persistent-sandbox limit.
177
+ - **A buffered `exec` runs at most 90 seconds** and raises `Kosa8::ExecTimeoutError`.
178
+ `box.exec(cmd, timeout_s: 30)` stops a command sooner, in the sandbox (1 to 90 whole seconds). Run long
175
179
  work in the background and poll it:
176
180
  `box.exec("nohup sh -c 'make test > /workspace/test.log 2>&1; echo $? > /workspace/test.exit' &")`.
177
181
  - A request body (command and env) is at most 64 KiB; a file read or write is at most 8 MiB.
@@ -191,6 +195,28 @@ Kosa8::Cloud.new(api_key: tok["secret"]) # hand this to your end user, not your
191
195
 
192
196
  Every Cloud error is a `Kosa8::ApiError` carrying the server's stable `e.code`, its message and `e.body`.
193
197
 
198
+ ## The same thing in each language
199
+
200
+ The five kosa8 SDKs (Python `kosa8`, JS `@kosa8/sdk`, Go `kosa8-go`, Ruby `kosa8`, Java
201
+ `com.kosa8:kosa8`) call the same kosa8 Cloud API, but some names differ by language. The ones
202
+ that catch people switching:
203
+
204
+ | | Python | JS / TS | Go | Ruby | Java |
205
+ |---|---|---|---|---|---|
206
+ | client | `Kosa8Cloud()` | `new Kosa8Cloud()` | `kosa8.NewCloud(kosa8.CloudOptions{})`, returns `(cloud, err)` | `Kosa8::Cloud.new` | `Kosa8Cloud.builder().build()` |
207
+ | lifetime on create | `ttl_s=3600` | `{ ttlS: 3600 }` | `CloudCreateOptions{TTLSeconds: 3600}` | `ttl_s: 3600` | `new CloudOptions.Create().ttlSeconds(3600)` |
208
+ | persistent sandbox | `persistent=True` | `{ persistent: true }` | `CloudCreateOptions{Persistent: true}` | `persistent: true` | `.persistent(true)` |
209
+ | sandbox id | `box.id` | `box.id` | `box.ID()`, a method (`fmt.Println(box.ID)` prints a func value; `go vet` reports it) | `box.id` | `box.id()` |
210
+ | shell command | `box.exec("ls /")` | `box.exec('ls /')` | `box.ExecShell(ctx, "ls /", nil)` | `box.exec("ls /")` | `box.exec("ls /")` |
211
+ | argv command | `box.exec(["ls", "/"])` | `box.exec(['ls', '/'])` | `box.Exec(ctx, []string{"ls", "/"}, nil)` | `box.exec(["ls", "/"])` | `box.exec(List.of("ls", "/"))` |
212
+ | per-exec timeout | `exec(cmd, timeout_s=30)` | `exec(cmd, { timeoutS: 30 })` | `&kosa8.CloudExecOptions{TimeoutS: 30}` | `exec(cmd, timeout_s: 30)` | `exec(cmd, null, null, 30)` |
213
+ | exit code | `r.exit_code` | `r.exitCode` | `r.ExitCode`, an `*int` (nil when the cloud sent none; `r.OK()` checks for 0) | `r.exit_code` | `r.exitCode()`, an `Integer` (may be null) |
214
+ | files | `box.files.write` / `read_text` / `list` | `box.files.write` / `readText` / `list` | `box.Files().WriteString` / `ReadText` / `List(ctx, "")` | `box.files.write` / `read_text` / `list` | `box.files().write` / `readText` / `list()` |
215
+ | no such sandbox | `Kosa8NotFoundError` | `Kosa8NotFoundError` | `*kosa8.Error` with `Kind` `KindNotFound` (`errors.Is(err, kosa8.ErrNotFound)`) | `Kosa8::NotFoundError` | `Kosa8Exception.NotFoundException` |
216
+
217
+ In every SDK a non-zero exit is returned in the result, not raised. Only `/workspace` is sure to
218
+ persist between commands, unless the sandbox is persistent.
219
+
194
220
  ## Local daemon
195
221
 
196
222
  ```ruby
data/lib/kosa8/cloud.rb CHANGED
@@ -249,7 +249,9 @@ module Kosa8
249
249
  message = (msg_body.is_a?(Hash) ? msg_body["error"] : msg_body).to_s
250
250
  e = if exec_timeout?(status, code, message, path)
251
251
  said = message.empty? ? nil : message
252
- ExecTimeoutError.new("#{said || "request failed with HTTP #{status}"} (#{EXEC_HINT})",
252
+ # Its own timeout_s (exec(..., timeout_s:)) needs no hint about the 90 s limit.
253
+ hint = message.include?("timeout_s") ? "" : " (#{EXEC_HINT})"
254
+ ExecTimeoutError.new("#{said || "request failed with HTTP #{status}"}#{hint}",
253
255
  status: status, daemon_message: said, method: method, path: path,
254
256
  code: code || "exec_timeout")
255
257
  else
@@ -499,17 +501,25 @@ module Kosa8
499
501
  # Run a command and wait for it. A String runs through sh -c, an Array is
500
502
  # the argv. A sleeping sandbox wakes for it.
501
503
  #
502
- # Each exec runs in a fresh container: only /workspace persists, so cd,
503
- # export and packages installed elsewhere are gone by the next one. kosa8
504
- # Cloud stops a buffered exec after 90 s, raised as
505
- # Kosa8::ExecTimeoutError; run long work in the background
506
- # (nohup sh -c '...' > /workspace/job.log 2>&1 &) and poll it. The request
504
+ # Only /workspace is sure to persist: an exec may run in a fresh
505
+ # container (unless the sandbox is persistent), so cd, export and packages
506
+ # installed elsewhere may be gone by the next one. kosa8 Cloud stops a
507
+ # buffered exec after 90 s, raised as Kosa8::ExecTimeoutError; run long
508
+ # work in the background (nohup sh -c '...' > /workspace/job.log 2>&1 &)
509
+ # and poll it. +timeout_s+ (1 to 90 whole seconds) stops the command in
510
+ # the sandbox sooner, also as Kosa8::ExecTimeoutError. The request
507
511
  # (command and env) is at most 64 KiB (413 too_large).
508
- def exec(cmd, image: nil, env: nil)
512
+ def exec(cmd, image: nil, env: nil, timeout_s: nil)
509
513
  argv = cmd.is_a?(String) ? ["sh", "-c", cmd] : cmd.to_a
510
514
  body = { "cmd" => argv }
511
515
  body["image"] = image unless image.nil?
512
516
  body["env"] = env.map { |k, v| "#{k}=#{v}" } if env && !env.empty?
517
+ unless timeout_s.nil?
518
+ unless timeout_s.is_a?(Integer) && timeout_s.between?(1, Cloud::EXEC_LIMIT_S)
519
+ raise ArgumentError, "timeout_s must be a whole number of seconds from 1 to #{Cloud::EXEC_LIMIT_S}"
520
+ end
521
+ body["timeout_s"] = timeout_s
522
+ end
513
523
  r = @cloud.request("POST", path("/exec"), body)
514
524
  fc, rt = Kosa8.exec_failure(r["exit"])
515
525
  fc = r["failure_class"] if r["failure_class"].is_a?(String)
data/lib/kosa8/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kosa8
4
- VERSION = "0.5.2"
4
+ VERSION = "0.5.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kosa8
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kosa8