kobako 0.28.0 → 0.30.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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +377 -0
- data/Cargo.lock +181 -154
- data/README.md +99 -459
- data/ROADMAP.md +7 -1
- data/SECURITY.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +354 -0
- data/crates/kobako-runtime/Cargo.toml +38 -7
- data/crates/kobako-runtime/README.md +17 -8
- data/crates/kobako-runtime/src/dispatch.rs +9 -1
- data/crates/kobako-runtime/src/endpoint/buffer.rs +192 -0
- data/crates/kobako-runtime/src/endpoint/capture.rs +115 -0
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +253 -0
- data/crates/kobako-runtime/src/endpoint/invoke.rs +194 -0
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +238 -0
- data/crates/kobako-runtime/src/endpoint.rs +166 -0
- data/crates/kobako-runtime/src/filesystem/bind/fid.rs +82 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +236 -0
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +249 -0
- data/crates/kobako-runtime/src/filesystem/bind.rs +2665 -0
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +89 -0
- data/crates/kobako-runtime/src/filesystem/overlay/inode.rs +106 -0
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +285 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +135 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +263 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +135 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +57 -0
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +301 -0
- data/crates/kobako-runtime/src/filesystem/overlay.rs +3469 -0
- data/crates/kobako-runtime/src/filesystem/path.rs +21 -0
- data/crates/kobako-runtime/src/filesystem/pipe.rs +221 -0
- data/crates/kobako-runtime/src/filesystem/rdir.rs +101 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +85 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +22 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +1985 -0
- data/crates/kobako-runtime/src/filesystem/wrap.rs +224 -0
- data/crates/kobako-runtime/src/filesystem.rs +26 -0
- data/crates/kobako-runtime/src/grant.rs +20 -0
- data/crates/kobako-runtime/src/hashtable.rs +44 -0
- data/crates/kobako-runtime/src/lib.rs +16 -1
- data/crates/kobako-runtime/src/locking/seqlock.rs +66 -0
- data/crates/kobako-runtime/src/locking.rs +218 -0
- data/crates/kobako-runtime/src/mount.rs +105 -0
- data/crates/kobako-runtime/src/profile.rs +140 -4
- data/crates/kobako-runtime/src/runtime.rs +12 -0
- data/crates/kobako-runtime/src/sched.rs +210 -0
- data/crates/kobako-runtime/src/security.rs +696 -0
- data/crates/kobako-runtime/src/snapshot.rs +20 -5
- data/crates/kobako-runtime/src/vfs/attr.rs +162 -0
- data/crates/kobako-runtime/src/vfs/cred.rs +334 -0
- data/crates/kobako-runtime/src/vfs/dentry.rs +1412 -0
- data/crates/kobako-runtime/src/vfs/errno.rs +153 -0
- data/crates/kobako-runtime/src/vfs/file.rs +1915 -0
- data/crates/kobako-runtime/src/vfs/inode.rs +429 -0
- data/crates/kobako-runtime/src/vfs/kern_path.rs +217 -0
- data/crates/kobako-runtime/src/vfs/mount.rs +59 -0
- data/crates/kobako-runtime/src/vfs/namei.rs +5708 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +116 -0
- data/crates/kobako-runtime/src/vfs/path.rs +64 -0
- data/crates/kobako-runtime/src/vfs/readdir.rs +163 -0
- data/crates/kobako-runtime/src/vfs/stat.rs +157 -0
- data/crates/kobako-runtime/src/vfs/super_block.rs +122 -0
- data/crates/kobako-runtime/src/vfs/utimes.rs +289 -0
- data/crates/kobako-runtime/src/vfs.rs +60 -0
- data/crates/kobako-runtime/src/yielder.rs +11 -6
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +171 -0
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +276 -0
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +323 -0
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +16 -0
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +449 -0
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +611 -0
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +236 -0
- data/crates/kobako-transport/CHANGELOG.md +23 -0
- data/crates/kobako-transport/Cargo.toml +3 -3
- data/crates/kobako-transport/README.md +6 -5
- data/crates/kobako-transport/src/abi.rs +1 -1
- data/crates/kobako-transport/src/envelope/bytes.rs +33 -25
- data/crates/kobako-transport/src/envelope/call.rs +5 -5
- data/crates/kobako-transport/src/envelope/compile_reply.rs +2 -2
- data/crates/kobako-transport/src/envelope/error_record.rs +3 -3
- data/crates/kobako-transport/src/envelope/fault.rs +4 -3
- data/crates/kobako-transport/src/envelope/outcome.rs +3 -3
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -24
- data/crates/kobako-transport/src/envelope/reply.rs +34 -16
- data/crates/kobako-transport/src/envelope/request.rs +19 -9
- data/crates/kobako-transport/src/envelope/run.rs +1 -1
- data/crates/kobako-transport/src/envelope/unsupported.rs +2 -2
- data/crates/kobako-transport/src/envelope.rs +2 -2
- data/crates/kobako-wasmtime/CHANGELOG.md +265 -0
- data/crates/kobako-wasmtime/Cargo.toml +25 -19
- data/crates/kobako-wasmtime/README.md +13 -7
- data/crates/kobako-wasmtime/src/abi.rs +75 -49
- data/crates/kobako-wasmtime/src/ambient.rs +64 -6
- data/crates/kobako-wasmtime/src/cache.rs +188 -77
- data/crates/kobako-wasmtime/src/config.rs +77 -5
- data/crates/kobako-wasmtime/src/descriptors.rs +200 -0
- data/crates/kobako-wasmtime/src/driver.rs +70 -135
- data/crates/kobako-wasmtime/src/exports.rs +5 -16
- data/crates/kobako-wasmtime/src/guest.rs +38 -0
- data/crates/kobako-wasmtime/src/imports.rs +52 -0
- data/crates/kobako-wasmtime/src/instance_pre.rs +18 -46
- data/crates/kobako-wasmtime/src/invocation.rs +43 -74
- data/crates/kobako-wasmtime/src/lib.rs +5 -4
- data/crates/kobako-wasmtime/src/limiter.rs +6 -230
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +632 -0
- data/crates/kobako-wasmtime/src/preview1/differential.rs +486 -0
- data/crates/kobako-wasmtime/src/preview1/errno.rs +135 -0
- data/crates/kobako-wasmtime/src/preview1/fd.rs +1420 -0
- data/crates/kobako-wasmtime/src/preview1/memory.rs +172 -0
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +211 -0
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +3766 -0
- data/crates/kobako-wasmtime/src/preview1/path.rs +302 -0
- data/crates/kobako-wasmtime/src/preview1/poll.rs +381 -0
- data/crates/kobako-wasmtime/src/preview1/sock.rs +13 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite-expected.txt +6 -0
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +157 -0
- data/crates/kobako-wasmtime/src/preview1.rs +512 -0
- data/crates/kobako-wasmtime/src/trap.rs +45 -25
- data/crates/kobako-wasmtime/src/wasi.rs +333 -0
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +43 -0
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +1085 -0
- data/crates/kobako-wasmtime/tests/integration/main.rs +29 -0
- data/crates/kobako-wasmtime/tests/{preamble_flags.rs → integration/preamble_flags.rs} +4 -3
- data/crates/kobako-wasmtime/tests/{snippet_compile.rs → integration/snippet_compile.rs} +5 -4
- data/crates/kobako-wasmtime/tests/{support → integration/support}/mod.rs +9 -13
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +86 -0
- data/crates/kobako-wasmtime/tests/{unsupported_call.rs → integration/unsupported_call.rs} +2 -1
- data/crates/kobako-wasmtime/witx/typenames.witx +750 -0
- data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +441 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +4 -2
- data/ext/kobako/src/runtime/bridge.rs +154 -81
- data/ext/kobako/src/runtime/errors.rs +1 -1
- data/ext/kobako/src/runtime/escape.rs +67 -0
- data/ext/kobako/src/runtime/filesystem/grants.rs +107 -0
- data/ext/kobako/src/runtime/filesystem/ruby.rs +708 -0
- data/ext/kobako/src/runtime/filesystem.rs +208 -0
- data/ext/kobako/src/runtime/gvl.rs +49 -8
- data/ext/kobako/src/runtime/pinned_value.rs +31 -0
- data/ext/kobako/src/runtime/snapshot.rs +127 -0
- data/ext/kobako/src/runtime.rs +125 -199
- data/lib/kobako/catalog/extensions.rb +5 -5
- data/lib/kobako/catalog/flags.rb +2 -14
- data/lib/kobako/catalog/grants.rb +30 -0
- data/lib/kobako/catalog/handles.rb +6 -6
- data/lib/kobako/catalog/mount.rb +19 -0
- data/lib/kobako/catalog/services.rb +13 -2
- data/lib/kobako/catalog/snippets.rb +11 -15
- data/lib/kobako/catalog.rb +11 -0
- data/lib/kobako/codec/decoder.rb +10 -9
- data/lib/kobako/codec/ext_types.rb +4 -6
- data/lib/kobako/codec/handle_walk.rb +4 -6
- data/lib/kobako/codec/nesting.rb +7 -7
- data/lib/kobako/codec/state.rb +19 -3
- data/lib/kobako/codec/utils.rb +1 -1
- data/lib/kobako/codec/value_budget.rb +86 -0
- data/lib/kobako/codec.rb +7 -0
- data/lib/kobako/context.rb +40 -13
- data/lib/kobako/errors.rb +15 -14
- data/lib/kobako/filesystem.rb +69 -0
- data/lib/kobako/payload/arguments.rb +2 -2
- data/lib/kobako/pool.rb +6 -6
- data/lib/kobako/sandbox.rb +55 -27
- data/lib/kobako/sandbox_options.rb +39 -13
- data/lib/kobako/snippet/{binary.rb → bytecode.rb} +2 -2
- data/lib/kobako/snippet/source.rb +1 -1
- data/lib/kobako/snippet.rb +1 -1
- data/lib/kobako/transport/block_failure.rb +35 -0
- data/lib/kobako/transport/dispatcher.rb +33 -29
- data/lib/kobako/transport/exposure.rb +7 -9
- data/lib/kobako/transport/reflection.rb +79 -23
- data/lib/kobako/transport/run.rb +5 -5
- data/lib/kobako/transport/yielder.rb +9 -31
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +35 -0
- data/release-please-config.json +1 -0
- data/sig/kobako/catalog/extensions.rbs +3 -3
- data/sig/kobako/catalog/flags.rbs +0 -4
- data/sig/kobako/catalog/grants.rbs +13 -0
- data/sig/kobako/catalog/handles.rbs +3 -3
- data/sig/kobako/catalog/mount.rbs +24 -0
- data/sig/kobako/catalog/services.rbs +5 -1
- data/sig/kobako/catalog/snippets.rbs +4 -4
- data/sig/kobako/catalog.rbs +1 -0
- data/sig/kobako/codec/decoder.rbs +1 -1
- data/sig/kobako/codec/handle_walk.rbs +1 -1
- data/sig/kobako/codec/nesting.rbs +2 -2
- data/sig/kobako/codec/state.rbs +7 -1
- data/sig/kobako/codec/utils.rbs +1 -1
- data/sig/kobako/codec/value_budget.rbs +23 -0
- data/sig/kobako/codec.rbs +3 -2
- data/sig/kobako/context.rbs +14 -6
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +93 -0
- data/sig/kobako/guest_yielder.rbs +9 -0
- data/sig/kobako/payload/arguments.rbs +1 -1
- data/sig/kobako/pool.rbs +3 -3
- data/sig/kobako/runtime.rbs +21 -49
- data/sig/kobako/sandbox.rbs +9 -5
- data/sig/kobako/sandbox_options.rbs +25 -3
- data/sig/kobako/snapshot.rbs +20 -0
- data/sig/kobako/snippet/bytecode.rbs +12 -0
- data/sig/kobako/transport/block_failure.rbs +13 -0
- data/sig/kobako/transport/dispatcher.rbs +4 -4
- data/sig/kobako/transport/exposure.rbs +2 -2
- data/sig/kobako/transport/reflection.rbs +14 -1
- data/sig/kobako/transport/run.rbs +3 -3
- data/sig/kobako/transport/yielder.rbs +3 -7
- data/sig/kobako/transport.rbs +3 -3
- metadata +116 -15
- data/crates/kobako-wasmtime/src/capture.rs +0 -96
- data/crates/kobako-wasmtime/src/dispatch.rs +0 -107
- data/crates/kobako-wasmtime/src/guest_mem.rs +0 -265
- data/crates/kobako-wasmtime/src/io.rs +0 -286
- data/sig/kobako/snippet/binary.rbs +0 -12
data/README.md
CHANGED
|
@@ -2,573 +2,213 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://deepwiki.com/elct9620/kobako)
|
|
4
4
|
|
|
5
|
-
Kobako is a Ruby gem that
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
Kobako is a Ruby gem that runs untrusted mruby scripts in an in-process Wasm
|
|
6
|
+
sandbox: LLM-generated code, user formulas, student submissions, plugins. A
|
|
7
|
+
script reaches no host memory, file, network, or credential you did not hand it.
|
|
8
|
+
A Rust SDK offers the same sandbox to hosts in other languages.
|
|
8
9
|
|
|
9
10
|
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
│ Execution │ ◀─pipe─ │ puts / warn │
|
|
18
|
-
│ value │ ◀─last─ │ last expression │
|
|
19
|
-
│ stdout / stderr │ │ │
|
|
20
|
-
│ usage │ │ │
|
|
21
|
-
└──────────────────────┘ └──────────────────────┘
|
|
22
|
-
trusted untrusted
|
|
11
|
+
Host process Wasm guest
|
|
12
|
+
+---------------------+ +---------------------+
|
|
13
|
+
| Kobako::Sandbox |--eval-->| mruby interpreter |
|
|
14
|
+
| Services |<--call--| KV::Lookup.call(k) |
|
|
15
|
+
| Execution |<-result-| last expression |
|
|
16
|
+
+---------------------+ +---------------------+
|
|
17
|
+
trusted untrusted
|
|
23
18
|
```
|
|
24
19
|
|
|
25
20
|
## Requirements
|
|
26
21
|
|
|
22
|
+
The gem ships its Guest Binary and, on most platforms, a prebuilt native
|
|
23
|
+
extension.
|
|
24
|
+
|
|
27
25
|
| What | Which versions | When you need it |
|
|
28
26
|
|---|---|---|
|
|
29
|
-
| Ruby |
|
|
30
|
-
| Platform | Linux or
|
|
31
|
-
| Rust / Cargo | current stable | only where no prebuilt gem matches your platform
|
|
32
|
-
| WASI toolchain | vendored by the build | only when building
|
|
33
|
-
|
|
34
|
-
The precompiled `kobako.wasm` Guest Binary ships inside the gem, so end users do **not** need a WASI toolchain.
|
|
27
|
+
| Ruby | >= 3.3.0 | always |
|
|
28
|
+
| Platform | Linux, macOS, or Windows | always; Windows gaps are fixed last |
|
|
29
|
+
| Rust / Cargo | current stable | only where no prebuilt gem matches your platform |
|
|
30
|
+
| WASI toolchain | vendored by the build | only when building from a source checkout |
|
|
35
31
|
|
|
36
32
|
## Installation
|
|
37
33
|
|
|
38
|
-
|
|
34
|
+
Add the gem to your bundle.
|
|
39
35
|
|
|
40
36
|
```bash
|
|
41
37
|
bundle add kobako
|
|
42
|
-
# or
|
|
43
|
-
gem install kobako
|
|
44
38
|
```
|
|
45
39
|
|
|
46
40
|
## Quick Start
|
|
47
41
|
|
|
48
|
-
|
|
42
|
+
A Sandbox runs guest code and returns the record of that run.
|
|
49
43
|
|
|
50
44
|
```ruby
|
|
51
45
|
require "kobako"
|
|
52
46
|
|
|
53
47
|
sandbox = Kobako::Sandbox.new
|
|
54
|
-
sandbox.eval("1 + 2").value
|
|
48
|
+
sandbox.eval("1 + 2").value # => 3
|
|
55
49
|
```
|
|
56
50
|
|
|
57
51
|
```rust
|
|
58
52
|
use kobako::{Options, Sandbox};
|
|
59
53
|
|
|
60
|
-
// Options::default() is secure by default: the same caps as Ruby, hermetic isolation.
|
|
61
54
|
let sandbox = Sandbox::new("kobako.wasm", Options::default())?;
|
|
62
|
-
sandbox.eval("1 + 2")?.value()?;
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Each invocation returns the record of that one run: `Kobako::Execution` in Ruby, `Execution` in Rust. It carries the guest value alongside the run's output and resource usage. Nothing a run observes is stored on the Sandbox, so the same Sandbox serves concurrent invocations without them seeing each other.
|
|
66
|
-
|
|
67
|
-
The gem bundles its Guest Binary; a Rust host loads one explicitly — see [Frontends & Guest Binaries](#frontends--guest-binaries) for the packages and the Guest Binary variants.
|
|
68
|
-
|
|
69
|
-
## Frontends & Guest Binaries
|
|
70
|
-
|
|
71
|
-
Embedding kobako is two independent choices: the **host frontend** you build against, and the **Guest Binary** it runs.
|
|
72
|
-
|
|
73
|
-
```text
|
|
74
|
-
host frontend Guest Binary
|
|
75
|
-
Ruby gem ──┐ ┌── kobako.wasm (pure)
|
|
76
|
-
Rust SDK ──┼──┼── kobako+regexp.wasm
|
|
77
|
-
your own ──┘ └── kobako+json.wasm · your own
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
They compose freely, since any frontend loads any Guest Binary. A Ruby host can run a JSON-enabled guest, and a Rust host can run the pure default. How far down those choices go — and which ones a given starting point quietly makes for you — is laid out in [`docs/guides/assembly-levels.md`](docs/guides/assembly-levels.md).
|
|
81
|
-
|
|
82
|
-
### Host frontends
|
|
83
|
-
|
|
84
|
-
The host embeds the sandbox and owns the wire codec. Choose by your host language and how much of the contract you want pre-assembled.
|
|
85
|
-
|
|
86
|
-
| Frontend | Package | Add it | Best for |
|
|
87
|
-
|----------|---------|--------|----------|
|
|
88
|
-
| Ruby gem | `kobako` (RubyGems) | `gem install kobako` | A Ruby host — Services, Handles, snippets, and pooling out of the box |
|
|
89
|
-
| Rust SDK | `kobako` (crates.io) | `cargo add kobako` | A Rust host — the same behavior contract behind an idiomatic Rust API |
|
|
90
|
-
| Low-level crates | `kobako-wasmtime` + `kobako-runtime` + `kobako-transport` + `kobako-codec` | `cargo add` each | A custom host, or driving the wire directly in another language |
|
|
91
|
-
|
|
92
|
-
The Rust crates are documented on [crates.io](https://crates.io/crates/kobako); the Ruby gem is this README. Three runnable Rust hosts show the choice. [`plugin-rs`](examples/plugin-rs) builds on the SDK, and [`wire-rs`](examples/wire-rs) assembles a host by hand on the low-level crates. [`fixed-schema-rs`](examples/fixed-schema-rs) replaces the payload schema both of those keep, with a guest and a host that agree on protobuf.
|
|
93
|
-
|
|
94
|
-
### Pre-built Guest Binaries
|
|
95
|
-
|
|
96
|
-
The gem bundles the pure `kobako.wasm`. Regexp and JSON are opt-in capabilities compiled into separate variants, each attached to every [GitHub Release](https://github.com/elct9620/kobako/releases). Download a variant and point your host at it — `Sandbox.new(wasm_path:)` in Ruby, `Sandbox::new(path, ...)` in Rust. [`docs/guides/variants.md`](docs/guides/variants.md) lists every variant and what it adds.
|
|
97
|
-
|
|
98
|
-
```ruby
|
|
99
|
-
sandbox = Kobako::Sandbox.new(wasm_path: "kobako+full.wasm")
|
|
100
|
-
sandbox.eval('JSON.generate({ n: "42".to_i })').value # => "{\"n\":42}"
|
|
55
|
+
sandbox.eval("1 + 2")?.value()?; // => Value::Int(3)
|
|
101
56
|
```
|
|
102
57
|
|
|
103
|
-
|
|
58
|
+
These terms name what the rest of this README builds on.
|
|
104
59
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
| To decide | Read |
|
|
60
|
+
| Term | Meaning |
|
|
108
61
|
|---|---|
|
|
109
|
-
|
|
|
110
|
-
|
|
|
111
|
-
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
A shell may also define flags, switches the Host App sets for every invocation. A flag is critical by default, so a Guest Binary that does not recognise it raises `Kobako::UnsupportedFlagError` rather than run without it. Mark it `advisory: true` only when a script cannot tell the flag was skipped.
|
|
116
|
-
|
|
117
|
-
```ruby
|
|
118
|
-
sandbox.flag(:strict_mode) # critical: refused where unrecognised
|
|
119
|
-
sandbox.flag(:compile_cache, "on", advisory: true) # skipped where unrecognised
|
|
120
|
-
```
|
|
62
|
+
| Sandbox | the unit you configure once and invoke many times |
|
|
63
|
+
| Invocation | one run of guest code, by `#eval` or `#run` |
|
|
64
|
+
| Execution | the frozen record an Invocation leaves |
|
|
65
|
+
| Service | a host object the guest reaches by name |
|
|
66
|
+
| Mount | a Filesystem placed at a guest path |
|
|
121
67
|
|
|
122
|
-
##
|
|
68
|
+
## Features
|
|
123
69
|
|
|
124
|
-
|
|
70
|
+
Each feature below shows its smallest use; the [guides] hold the rest.
|
|
125
71
|
|
|
126
|
-
|
|
127
|
-
|------|------------|---------|
|
|
128
|
-
| Sandbox | `Kobako::Sandbox` | The unit you configure once and invoke many times. |
|
|
129
|
-
| Invocation | — | One run of guest code, by `#eval` or `#run`. |
|
|
130
|
-
| Execution | `Kobako::Execution` | The frozen record an Invocation leaves. |
|
|
131
|
-
| Service | — | A host object the guest reaches by name, such as `MyService::KV`. |
|
|
132
|
-
| Context | `Kobako::Context` | The per-Invocation object the optional block receives. |
|
|
133
|
-
| Snippet | — | Guest source or bytecode replayed at the start of every Invocation. |
|
|
134
|
-
| Handle | `Kobako::Handle` | An opaque reference to a host object the wire cannot carry by value. |
|
|
135
|
-
| Block | — | Guest code passed alongside a Service call. |
|
|
136
|
-
|
|
137
|
-
A Sandbox keeps nothing from one Invocation to the next. An Execution holds what the run produced, wrote, and consumed: `#value`, output captures, and `#usage`. A failed run raises, carrying its Execution on the error's `#execution`. A Service is the guest's only route to a host resource. A Context's `ctx.bind` supplies a Service object for that one run. A Handle names its object only within the Invocation that issued it. A Block stays in the guest; each `yield` to it is a synchronous round-trip.
|
|
138
|
-
|
|
139
|
-
## Usage
|
|
140
|
-
|
|
141
|
-
Each section below stands on its own; this is the order they build in.
|
|
142
|
-
|
|
143
|
-
| If you want to | Read |
|
|
144
|
-
|---|---|
|
|
145
|
-
| let guest code call into your host | [Services](#services), [Per-Invocation Bindings](#per-invocation-bindings), [Service Blocks](#service-blocks) |
|
|
146
|
-
| see what a run produced or why it failed | [Output Capture](#output-capture), [Error Handling](#error-handling), [Invocation Lifecycle](#invocation-lifecycle) |
|
|
147
|
-
| bound what a run may consume | [Resource Limits](#resource-limits), [Concurrency](#concurrency), [Pooling](#pooling) |
|
|
148
|
-
| hand host objects across the boundary | [Handle Management](#handle-management) |
|
|
149
|
-
| ship guest code with the Sandbox | [Snippets & Entrypoints](#snippets--entrypoints), [Extensions](#extensions) |
|
|
72
|
+
[guides]: https://github.com/elct9620/kobako/tree/main/docs/guides
|
|
150
73
|
|
|
151
74
|
### Services
|
|
152
75
|
|
|
153
|
-
`bind`
|
|
76
|
+
`bind` makes a host object reachable from the guest at a constant name.
|
|
154
77
|
|
|
155
78
|
```ruby
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def initialize(name:)
|
|
160
|
-
@name = name
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
sandbox.bind("Project::User", User.new(name: "alice"))
|
|
165
|
-
sandbox.bind("KV::Lookup", ->(key) { redis.get(key) })
|
|
166
|
-
|
|
167
|
-
sandbox.eval(<<~RUBY)
|
|
168
|
-
Project::User.name # => "alice"
|
|
169
|
-
KV::Lookup.call("user_42") # => "..."
|
|
170
|
-
RUBY
|
|
79
|
+
sandbox.bind("KV::Lookup", ->(key) { redis.get(key) })
|
|
80
|
+
sandbox.eval('KV::Lookup.call("user_42")').value
|
|
171
81
|
```
|
|
172
82
|
|
|
173
|
-
|
|
83
|
+
A Service can also be filled per Invocation, take a guest block, and hand back
|
|
84
|
+
host objects as opaque Handles.
|
|
174
85
|
|
|
175
|
-
###
|
|
86
|
+
### Mounts
|
|
176
87
|
|
|
177
|
-
|
|
88
|
+
`mount` gives the guest's `File` and `Dir` a Filesystem at one path, read-only by default.
|
|
178
89
|
|
|
179
90
|
```ruby
|
|
180
|
-
sandbox.
|
|
181
|
-
|
|
182
|
-
sandbox.eval("Req::Current.user_id") { |ctx| ctx.bind("Req::Current", request) }
|
|
91
|
+
sandbox.mount("/config", Kobako::Filesystem::Tmpfs.new("app.yml" => yaml))
|
|
92
|
+
sandbox.eval('File.read("/config/app.yml")').value
|
|
183
93
|
```
|
|
184
94
|
|
|
185
|
-
|
|
95
|
+
| Kind | Holds |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `Tmpfs` | files in memory |
|
|
98
|
+
| `Bind` | one host directory |
|
|
99
|
+
| `Overlay` | read-only Layers under a Tmpfs |
|
|
186
100
|
|
|
187
|
-
###
|
|
101
|
+
### Execution Records
|
|
188
102
|
|
|
189
|
-
|
|
103
|
+
An Execution carries the value, the output, and the usage. A failed run raises
|
|
104
|
+
one of three classes, each carrying its Execution.
|
|
190
105
|
|
|
191
106
|
```ruby
|
|
192
|
-
execution = sandbox.eval(
|
|
193
|
-
|
|
194
|
-
warn "be careful"
|
|
195
|
-
42
|
|
196
|
-
RUBY
|
|
197
|
-
|
|
198
|
-
execution.value # => 42
|
|
199
|
-
execution.stdout # => "hello\n"
|
|
200
|
-
execution.stderr # => "be careful\n"
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
### Error Handling
|
|
107
|
+
execution = sandbox.eval('puts "hi"; 42')
|
|
108
|
+
execution.stdout # => "hi\n"
|
|
204
109
|
|
|
205
|
-
Every invocation either returns an Execution or raises exactly one of three classes, so you can route faults without inspecting messages. The full taxonomy lives in [`lib/kobako/errors.rb`](lib/kobako/errors.rb).
|
|
206
|
-
|
|
207
|
-
```ruby
|
|
208
110
|
begin
|
|
209
111
|
sandbox.eval(script)
|
|
210
|
-
rescue Kobako::TrapError
|
|
211
|
-
|
|
212
|
-
rescue Kobako::ServiceError
|
|
213
|
-
# A host Service call failed and the script did not rescue it.
|
|
214
|
-
rescue Kobako::SandboxError => e
|
|
215
|
-
# The script raised, failed to compile, or returned an unrepresentable value,
|
|
216
|
-
# or the Guest Binary could not start the run.
|
|
217
|
-
logger.warn(e.execution.stderr) # the failed run's own captures and usage
|
|
112
|
+
rescue Kobako::TrapError, Kobako::ServiceError, Kobako::SandboxError => e
|
|
113
|
+
e.execution&.stderr
|
|
218
114
|
end
|
|
219
115
|
```
|
|
220
116
|
|
|
221
|
-
Each of these carries the failed run's Execution on `#execution`. A rescue reads that run's output and usage exactly as a successful caller reads them off the returned one. `#failed?` keeps the two apart when both `#value` are `nil` — a script whose last expression was `nil` versus one that never produced a value. A failure caught before the guest ran at all — a host pre-flight refusal — leaves `#execution` `nil`, since there is no run to report.
|
|
222
|
-
|
|
223
|
-
| Class | Parent | Trigger |
|
|
224
|
-
|---------------------------------|----------------|------------------------------------------------------|
|
|
225
|
-
| `Kobako::TimeoutError` | `TrapError` | Per-invocation `timeout` exhausted |
|
|
226
|
-
| `Kobako::MemoryLimitError` | `TrapError` | Per-invocation `memory_limit` exhausted |
|
|
227
|
-
| `Kobako::NoServiceError` | `ServiceError` | Nothing bound there, a dead Handle, or a method the guest may not call |
|
|
228
|
-
| `Kobako::ServiceArgumentError` | `ServiceError` | The arguments did not fit the Service method |
|
|
229
|
-
| `Kobako::HandleExhaustedError` | `SandboxError` | Handle counter reached its 2³¹ − 1 cap |
|
|
230
|
-
| `Kobako::BytecodeError` | `SandboxError` | `#preload(binary:)` failed RITE validation at replay |
|
|
231
|
-
| `Kobako::UnsupportedFlagError` | `SandboxError` | The Guest Binary does not recognise a critical `#flag`; no script ran |
|
|
232
|
-
| `Kobako::UndefinedEntrypointError` | `SandboxError` | `#run` named a constant no snippet defined; carries `#name` and `#available` |
|
|
233
|
-
|
|
234
|
-
`SandboxError` and `ServiceError` carry structured `origin` / `klass` / `backtrace_lines` fields when the guest produced a panic envelope.
|
|
235
|
-
|
|
236
117
|
### Resource Limits
|
|
237
118
|
|
|
238
|
-
Each
|
|
119
|
+
Each Invocation runs under caps, and reaching one ends that run alone.
|
|
239
120
|
|
|
240
121
|
```ruby
|
|
241
|
-
|
|
242
|
-
timeout: 5.0, # seconds, default 60.0
|
|
243
|
-
memory_limit: 10 * 1024 * 1024, # bytes, default 1 MiB
|
|
244
|
-
stdout_limit: 64 * 1024, # bytes, default 1 MiB
|
|
245
|
-
stderr_limit: 64 * 1024
|
|
246
|
-
)
|
|
122
|
+
Kobako::Sandbox.new(timeout: 5.0, memory_limit: 10 * 1024 * 1024)
|
|
247
123
|
```
|
|
248
124
|
|
|
249
|
-
| Cap
|
|
250
|
-
|----------------|----------------------------|---------|
|
|
251
|
-
| `timeout` | `Kobako::TimeoutError` | 60.0 s |
|
|
252
|
-
| `memory_limit` | `Kobako::MemoryLimitError` | 1 MiB |
|
|
253
|
-
| `stdout_limit` | output clipped (no raise) | 1 MiB |
|
|
254
|
-
| `stderr_limit` | output clipped (no raise) | 1 MiB |
|
|
255
|
-
|
|
256
|
-
`memory_limit` covers the per-invocation `memory.grow` delta from the entry baseline, so a Sandbox reused across invocations does not silently accumulate against a global budget.
|
|
257
|
-
|
|
258
|
-
Beyond the four caps, `profile:` requests the Sandbox's isolation posture on the `:permissive` < `:hermetic` ladder (default `:hermetic`). `:hermetic` denies the guest ambient time and entropy; `:permissive` lets the guest's `wasi:clocks` / `wasi:random` read live host sources, an explicit trade of reproducibility. Filesystem, environment, and network stay unreachable under either. The request is also a floor: construction fails with `Kobako::SetupError` on a runtime that declares a weaker posture than requested. See [`docs/guides/security.md`](docs/guides/security.md) § Isolation Profiles.
|
|
259
|
-
|
|
260
|
-
### Concurrency
|
|
261
|
-
|
|
262
|
-
A Sandbox keeps no state from any run, so concurrent Threads may invoke distinct Sandboxes or share a single one. Each invocation owns its Handles, captures, and usage either way. One Thread still runs one invocation at a time. Sharing a Sandbox adds a single obligation.
|
|
263
|
-
|
|
264
|
-
| What you bind | Who reaches it | What it owes |
|
|
265
|
-
|---|---|---|
|
|
266
|
-
| Bound once at setup | every Thread sharing the Sandbox | it must itself be thread-safe |
|
|
267
|
-
| Supplied per invocation — `ctx.bind`, or an Extension `provider:` | that invocation alone | nothing |
|
|
268
|
-
|
|
269
|
-
#### GVL Modes
|
|
270
|
-
|
|
271
|
-
By default an invocation holds Ruby's GVL for its whole span, so guest execution across Threads serializes. `gvl: :release` drops the GVL for the guest span and re-acquires it for each guest→host dispatch, running guest code in parallel across Threads.
|
|
272
|
-
|
|
273
|
-
```ruby
|
|
274
|
-
sandbox = Kobako::Sandbox.new(gvl: :release)
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
The mode is per-Sandbox and fixed at construction; it changes scheduling only, leaving isolation, Handle lifetimes, captures, and outcomes identical. `:hold` remains the default because releasing pays a handoff cost at every dispatch. Compute-bound scripts scale with Thread count, while dispatch-heavy ones match or trail `:hold`. `rake bench:gvl_scheduling` measures both ends on your own hardware.
|
|
278
|
-
|
|
279
|
-
### Invocation Lifecycle
|
|
280
|
-
|
|
281
|
-
One Sandbox serves many invocations. Service bindings and preloaded snippets persist across calls; everything a run produces — Handles, captured output, memory delta — belongs to that run alone.
|
|
282
|
-
|
|
283
|
-
```
|
|
284
|
-
───────────── setup phase (mutable) ─────────────
|
|
285
|
-
|
|
286
|
-
sandbox = Kobako::Sandbox.new
|
|
287
|
-
sandbox.bind("KV::Lookup", ...)
|
|
288
|
-
sandbox.bind("Req::Current") # fillable: object comes later
|
|
289
|
-
sandbox.preload(code: ..., name: :Adder)
|
|
290
|
-
sandbox.preload(code: ..., name: :Greeter)
|
|
291
|
-
|
|
292
|
-
│
|
|
293
|
-
▼
|
|
294
|
-
|
|
295
|
-
═════════════════ seal point ═════════════════
|
|
296
|
-
First #eval or #run freezes the Service registry
|
|
297
|
-
and snippet table. Further bind / preload now
|
|
298
|
-
raise ArgumentError.
|
|
299
|
-
|
|
300
|
-
│
|
|
301
|
-
▼
|
|
302
|
-
|
|
303
|
-
──────────────── invocation N ───────────────────
|
|
304
|
-
|
|
305
|
-
1. resolve this run's bindings, before the guest starts:
|
|
306
|
-
ctx.bind("Req::Current", ...) fills the fillable
|
|
307
|
-
provider: backends yield this run's object
|
|
308
|
-
|
|
309
|
-
2. start from the canonical boot state
|
|
310
|
-
(mruby pre-initialized into the artifact at build time)
|
|
311
|
-
|
|
312
|
-
3. replay snippets (in insertion order):
|
|
313
|
-
:Adder → defines Adder
|
|
314
|
-
:Greeter → defines Greeter
|
|
315
|
-
|
|
316
|
-
4. dispatch: eval(source) or run(:Target, *args, **kwargs)
|
|
317
|
-
|
|
318
|
-
5. return the run's Execution to host
|
|
319
|
-
|
|
320
|
-
6. discard the instance; per-invocation state ends with it:
|
|
321
|
-
· Handles invalidated
|
|
322
|
-
· captures frozen into the Execution
|
|
323
|
-
· memory delta zeroed
|
|
324
|
-
|
|
325
|
-
Services + snippets persist; invocation N+1 repeats.
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
For workloads that must be isolated from each other, construct a fresh `Kobako::Sandbox` per scope: per tenant, per student submission, per agent session. wasmtime's Engine and the compiled Module are cached at process scope, so additional Sandboxes amortize cold-start cost automatically.
|
|
329
|
-
|
|
330
|
-
### Pooling
|
|
331
|
-
|
|
332
|
-
For hosts that serve many short invocations, `Kobako::Pool` keeps a bounded set of warm, identically set-up Sandboxes. It hands each one to a single exclusive holder at a time. Construction forwards every `Sandbox.new` keyword verbatim; the optional block is the per-Sandbox setup window and runs exactly once per constructed Sandbox.
|
|
333
|
-
|
|
334
|
-
```ruby
|
|
335
|
-
pool = Kobako::Pool.new(slots: 4) do |sandbox|
|
|
336
|
-
sandbox.bind("KV::Lookup", ->(key) { redis.get(key) })
|
|
337
|
-
end
|
|
338
|
-
|
|
339
|
-
pool.with { |sandbox| sandbox.eval(%(KV::Lookup.call("user_42"))).value }
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
#### Options and lifetime
|
|
343
|
-
|
|
344
|
-
| Option | Meaning | Default |
|
|
345
|
-
|--------|---------|---------|
|
|
346
|
-
| `slots:` | Upper bound on constructed Sandboxes | required |
|
|
347
|
-
| `checkout_timeout:` | Seconds `#with` waits for a free Sandbox; `nil` waits indefinitely | 5.0 |
|
|
348
|
-
|
|
349
|
-
Sandboxes construct lazily on first demand. `#with` yields a Sandbox and returns the block's value, and at block exit the Sandbox returns to the pool, whatever the block raised. A checkout that waits past `checkout_timeout` raises `Kobako::PoolTimeoutError`. There is no teardown verb — a Pool releases everything with its own reachability.
|
|
350
|
-
|
|
351
|
-
#### Pool Trade-offs
|
|
352
|
-
|
|
353
|
-
| It gives you | It does not give you |
|
|
125
|
+
| Cap | Default |
|
|
354
126
|
|---|---|
|
|
355
|
-
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
### Service Blocks
|
|
360
|
-
|
|
361
|
-
A Service method can accept a guest-supplied block via `&blk` and `yield` into it. The block body runs inside the Wasm guest; `break` / `next` / exceptions follow normal Ruby semantics, scoped to the single dispatch.
|
|
362
|
-
|
|
363
|
-
```ruby
|
|
364
|
-
sandbox.bind("Seq::Map", ->(items, &blk) { items.map(&blk) })
|
|
127
|
+
| `timeout` | 60 s |
|
|
128
|
+
| `memory_limit` | 4 MiB |
|
|
129
|
+
| `stdout_limit`, `stderr_limit` | 1 MiB |
|
|
130
|
+
| `open_file_limit` | 1024 |
|
|
365
131
|
|
|
366
|
-
|
|
367
|
-
# => [2, 4, 6]
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
A `yield` that cannot complete raises at the Service's call site, so the Service may rescue it. Left unrescued, a `BlockError` hands the guest its own exception back; the other two fail the Service call.
|
|
371
|
-
|
|
372
|
-
| Class | Trigger at `yield` |
|
|
373
|
-
|----------------------------|---------------------------------------|
|
|
374
|
-
| `Kobako::BlockError` | The guest block raised |
|
|
375
|
-
| `Kobako::YieldValueError` | An argument cannot cross to the guest |
|
|
376
|
-
| `Kobako::UnsupportedError` | The Guest Binary serves no block |
|
|
377
|
-
|
|
378
|
-
### Handle Management
|
|
132
|
+
### Concurrency
|
|
379
133
|
|
|
380
|
-
A
|
|
134
|
+
A Sandbox keeps nothing between runs, so Threads may share one. `gvl:
|
|
135
|
+
:release` runs guest code in parallel, and `Kobako::Pool` keeps warm Sandboxes.
|
|
381
136
|
|
|
382
137
|
```ruby
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
def greet = "hi, #{@name}"
|
|
386
|
-
end
|
|
387
|
-
|
|
388
|
-
sandbox.bind("Factory::Make", ->(name) { Greeter.new(name) })
|
|
389
|
-
|
|
390
|
-
sandbox.eval('Factory::Make.call("Bob").greet').value # => "hi, Bob" (Handle round-trip inside guest)
|
|
391
|
-
sandbox.eval('Factory::Make.call("Bob")').value # => #<Greeter @name="Bob"> (Handle restoration)
|
|
138
|
+
pool = Kobako::Pool.new(slots: 4) { |sandbox| sandbox.bind("KV::Lookup", lookup) }
|
|
139
|
+
pool.with { |sandbox| sandbox.eval(script).value }
|
|
392
140
|
```
|
|
393
141
|
|
|
394
|
-
|
|
142
|
+
### Snippets
|
|
395
143
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
Each dispatch that hands back a non-wire-representable object allocates a *new* Handle — kobako never deduplicates by object identity. This is most visible with fluent / builder APIs. An `ActiveRecord::Relation` chain `spawn`s a fresh relation at each step, so every hop is an independent dispatch that binds its own Handle:
|
|
399
|
-
|
|
400
|
-
```
|
|
401
|
-
guest chain host (Catalog::Handles, one invocation)
|
|
402
|
-
─────────── ─────────────────────────────────────────
|
|
403
|
-
User.where(active: true) ─call──▶ Relation #1 (fresh clone) bound ▶ Handle 1
|
|
404
|
-
◀─Handle 1
|
|
405
|
-
.order(:created_at) ─call──▶ Relation #2 (fresh clone) bound ▶ Handle 2
|
|
406
|
-
◀─Handle 2
|
|
407
|
-
.limit(10) ─call──▶ Relation #3 (fresh clone) bound ▶ Handle 3
|
|
408
|
-
◀─Handle 3
|
|
409
|
-
|
|
410
|
-
3 hops ─▶ 3 dispatches ─▶ 3 distinct relations ─▶ 3 Handles
|
|
411
|
-
all stay live until the invocation ends, then reset together
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
This is deliberate, not a leak. Handle IDs run to 2³¹ − 1 per invocation and reset between invocations, so even deep chains stay far inside the range. Two consequences are worth keeping in mind. The same host object handed back twice yields two *different* Handles, and the guest cannot tell they alias. Every intermediate Handle stays live until the invocation ends, since there is no per-Handle release.
|
|
415
|
-
|
|
416
|
-
### Snippets & Entrypoints
|
|
417
|
-
|
|
418
|
-
`Sandbox#preload` registers named mruby snippets that replay into every invocation's canonical boot state; `Sandbox#run(:Target, *args, **kwargs)` dispatches into a top-level `Object` constant defined by those snippets.
|
|
144
|
+
`preload` replays guest code into every Invocation, and `run` calls what it
|
|
145
|
+
defined. An Extension pairs such code with a host backend.
|
|
419
146
|
|
|
420
147
|
```ruby
|
|
421
|
-
sandbox =
|
|
422
|
-
sandbox.
|
|
423
|
-
sandbox.preload(code: 'Greeter = ->(opts) { "hello, #{opts[:name]}" }', name: :Greeter)
|
|
424
|
-
|
|
425
|
-
sandbox.run(:Adder, 2, 3).value # => 5
|
|
426
|
-
sandbox.run(:Greeter, name: "world").value # => "hello, world"
|
|
148
|
+
sandbox.preload(code: "Adder = ->(a, b) { a + b }", name: :Adder)
|
|
149
|
+
sandbox.run(:Adder, 2, 3).value # => 5
|
|
427
150
|
```
|
|
428
151
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
A target no snippet defined raises `Kobako::UndefinedEntrypointError`. Its `#available` lists the top-level constants the snippets did contribute, so the name is corrected from the error rather than by reading the guest source.
|
|
432
|
-
|
|
433
|
-
```
|
|
434
|
-
per-invocation replay (every #eval / #run, snippets in insertion order):
|
|
435
|
-
|
|
436
|
-
canonical boot state
|
|
437
|
-
│
|
|
438
|
-
├──▶ replay :Adder (defines Adder)
|
|
439
|
-
│
|
|
440
|
-
├──▶ replay :Greeter (defines Greeter)
|
|
441
|
-
│
|
|
442
|
-
└──▶ eval(source) -or- run(:Target, *args, **kwargs)
|
|
443
|
-
│
|
|
444
|
-
▼
|
|
445
|
-
return the Execution, then discard the instance
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
#### Payload Forms
|
|
449
|
-
|
|
450
|
-
`#preload` accepts two payload forms:
|
|
451
|
-
|
|
452
|
-
| Form | Signature | Snippet name source | Validation timing |
|
|
453
|
-
|----------|--------------------------------------|---------------------------------------|----------------------------------------------------------------------------|
|
|
454
|
-
| Source | `preload(code: "...", name: :Const)` | The `name:` keyword | First invocation's replay; compile errors raise `Kobako::SandboxError` |
|
|
455
|
-
| Bytecode | `preload(binary: bytes)` | Read from the bytecode's `debug_info` | First invocation's replay; structural failure raises `Kobako::BytecodeError` |
|
|
456
|
-
|
|
457
|
-
Use the source form for snippets authored in your repo; use the bytecode form when snippets ship as build artifacts from a separate `mrbc` pipeline. Source is compiled once, before the first invocation, and replays as bytecode from then on, so the bytecode form saves only that one compile. Both replay through the same per-invocation path, so no snippet content failure surfaces at `#preload` — force the first replay with a no-op invocation (e.g. `sandbox.eval("nil")`) when you want validation before real traffic.
|
|
152
|
+
### Guest Binaries
|
|
458
153
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
An Extension teaches the guest a native-style constant by pairing a guest idiom (`source`) with an optional host `backend`. `Sandbox#install` composes the two through the existing `#preload` and `#bind` verbs, adding no wire or Guest Binary surface. Pure operations run in-guest with no round-trip. The rest dispatch to the backend under the same isolation and reflection guarantees as any bound Service ([`docs/guides/extensions.md`](docs/guides/extensions.md)).
|
|
154
|
+
The gem bundles the pure `kobako.wasm`. Each [GitHub Release] adds variants
|
|
155
|
+
with Regexp or JSON, and you can build your own in Rust.
|
|
462
156
|
|
|
463
157
|
```ruby
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
extend Kobako::Proxy
|
|
467
|
-
def self.basename(path) = path.split("/").last || "" # pure: runs in-guest
|
|
468
|
-
# read / write are undefined here, so they dispatch to the backend
|
|
469
|
-
end
|
|
470
|
-
MRUBY
|
|
471
|
-
|
|
472
|
-
sandbox.install(
|
|
473
|
-
Kobako::Extension.new(
|
|
474
|
-
name: :File,
|
|
475
|
-
source: FILE,
|
|
476
|
-
backend: Kobako::Extension::Backend.new(
|
|
477
|
-
path: "File",
|
|
478
|
-
provider: -> { OverlayFileSystem.new(root) } # invoked once per invocation
|
|
479
|
-
)
|
|
480
|
-
)
|
|
481
|
-
)
|
|
482
|
-
|
|
483
|
-
sandbox.eval('File.read("sample.txt")') # dispatches to the backend's #read
|
|
158
|
+
sandbox = Kobako::Sandbox.new(wasm_path: "kobako+full.wasm")
|
|
159
|
+
sandbox.eval('JSON.generate({ n: 42 })').value # => "{\"n\":42}"
|
|
484
160
|
```
|
|
485
161
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
| Keyword | Lifetime |
|
|
489
|
-
|-------------|-----------------------------------------------------------------------------|
|
|
490
|
-
| `object:` | One object, shared by every invocation |
|
|
491
|
-
| `provider:` | A no-argument callable invoked per invocation |
|
|
492
|
-
| neither | A fillable, standing for `Kobako::Unresolved` until `ctx.bind` supplies the run's object |
|
|
493
|
-
|
|
494
|
-
A writable backend takes `provider:`, so its state cannot leak across runs. kobako ships no concrete Extension; the idiom and backend are yours. The [overlay VFS example](examples/vfs/) is a worked `File` that reads through to disk while protecting it from guest writes.
|
|
162
|
+
[GitHub Release]: https://github.com/elct9620/kobako/releases
|
|
495
163
|
|
|
496
164
|
## Security
|
|
497
165
|
|
|
498
|
-
kobako isolates the guest, but
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
scoped to the task, not a capable one whose other methods leak more than you intend.
|
|
166
|
+
kobako isolates the guest, but it reaches whatever you `bind` or `mount`. Bind
|
|
167
|
+
an object built for the task, since every public method its class defines is
|
|
168
|
+
exposed.
|
|
502
169
|
|
|
503
170
|
```ruby
|
|
504
|
-
class ThemeReader
|
|
171
|
+
class ThemeReader # only #color is reachable
|
|
505
172
|
def color = AppConfig.theme.color
|
|
506
173
|
end
|
|
507
174
|
|
|
508
|
-
sandbox
|
|
509
|
-
sandbox.bind("Cfg::Settings", ThemeReader.new) # not: bind("Cfg::Settings", AppConfig)
|
|
510
|
-
|
|
511
|
-
sandbox.eval('Cfg::Settings.color').value # => "#3366ff" — any other method raises Kobako::NoServiceError
|
|
175
|
+
sandbox.bind("Cfg::Settings", ThemeReader.new) # not AppConfig itself
|
|
512
176
|
```
|
|
513
177
|
|
|
514
|
-
Guest code can name any `MyService::KV` path, but a forged name only resolves to
|
|
515
|
-
something you bound. What you bind is the real authorization gate. An object can also
|
|
516
|
-
narrow its own surface with a private `respond_to_guest?`. Give each trust context its own
|
|
517
|
-
Sandbox. [`docs/guides/security.md`](docs/guides/security.md) covers the rest: self-gating
|
|
518
|
-
objects, validating untrusted input, default-deny external effects, and the return surface.
|
|
519
|
-
|
|
520
178
|
## Performance
|
|
521
179
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
| Phase | Cost |
|
|
525
|
-
|--------------------------------------------------------------|-----------------------|
|
|
526
|
-
| First `Sandbox.new` ever for a Guest Binary (Module JIT, then disk-cached) | ~500 ms once per machine |
|
|
527
|
-
| First `Sandbox.new` in a fresh process (`.cwasm` cache warm) | ~3 ms one-time |
|
|
528
|
-
| Subsequent `Sandbox.new` (caches warm) | ~3 µs |
|
|
529
|
-
| Warm `#eval` with one Service call on a reused Sandbox | ~70 µs |
|
|
530
|
-
| Warm `#run(:Entrypoint, ...)` dispatch | ~79 µs |
|
|
531
|
-
| Service call amortized inside one invocation | ~5.8 µs |
|
|
532
|
-
| Snippet replay per invocation | ~7.0 µs each |
|
|
533
|
-
| Per additional idle Sandbox (RSS) | < 1 KB |
|
|
180
|
+
Figures are order-of-magnitude on macOS arm64 with Ruby 3.4.7; ratios hold
|
|
181
|
+
across machines. The [benchmark] suite gates every release on a +10% regression.
|
|
534
182
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
#### Throughput Levers
|
|
538
|
-
|
|
539
|
-
| Choice | What it changes |
|
|
183
|
+
| Phase | Cost |
|
|
540
184
|
|---|---|
|
|
541
|
-
|
|
|
542
|
-
|
|
|
543
|
-
| `
|
|
185
|
+
| first `Sandbox.new` per machine (JIT, then disk-cached) | ~500 ms |
|
|
186
|
+
| later `Sandbox.new` | ~3 µs |
|
|
187
|
+
| warm `#eval` with one Service call | ~62 µs |
|
|
544
188
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
```bash
|
|
548
|
-
bundle exec rake bench # every gated regression benchmark (~5-8 min)
|
|
549
|
-
```
|
|
189
|
+
[benchmark]: https://github.com/elct9620/kobako/tree/main/benchmark
|
|
550
190
|
|
|
551
191
|
## Development
|
|
552
192
|
|
|
553
|
-
|
|
193
|
+
The release gate compiles everything and runs every test and lint.
|
|
554
194
|
|
|
555
195
|
```bash
|
|
556
|
-
bin/setup
|
|
557
|
-
bundle exec rake
|
|
196
|
+
bin/setup # install dependencies
|
|
197
|
+
bundle exec rake # the release gate
|
|
198
|
+
bin/console # IRB with the gem loaded
|
|
199
|
+
bundle exec rake install # install the local checkout
|
|
558
200
|
```
|
|
559
201
|
|
|
560
|
-
|
|
202
|
+
The first compile builds mruby and the Guest Binary through the [beni] gem.
|
|
561
203
|
|
|
562
|
-
|
|
204
|
+
[beni]: https://github.com/elct9620/beni
|
|
563
205
|
|
|
564
|
-
|
|
206
|
+
## Contributing
|
|
565
207
|
|
|
566
|
-
|
|
208
|
+
Bug reports and pull requests are welcome at the [repository]. Open an issue
|
|
209
|
+
before a non-trivial change, so we can agree on its scope.
|
|
567
210
|
|
|
568
|
-
|
|
569
|
-
|---|---|---|
|
|
570
|
-
| Gem | the Ruby gem, with the bundled `kobako.wasm` | `v*` |
|
|
571
|
-
| Crates | the linked guest-crate group | `<component>-v*` |
|
|
211
|
+
[repository]: https://github.com/elct9620/kobako
|
|
572
212
|
|
|
573
213
|
## License
|
|
574
214
|
|