kobako 0.5.0-arm64-darwin

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.release-please-manifest.json +1 -0
  3. data/CHANGELOG.md +29 -0
  4. data/LICENSE +201 -0
  5. data/README.md +408 -0
  6. data/data/kobako.wasm +0 -0
  7. data/lib/kobako/3.3/kobako.bundle +0 -0
  8. data/lib/kobako/3.4/kobako.bundle +0 -0
  9. data/lib/kobako/4.0/kobako.bundle +0 -0
  10. data/lib/kobako/capture.rb +43 -0
  11. data/lib/kobako/catalog/handles.rb +107 -0
  12. data/lib/kobako/catalog/namespaces.rb +99 -0
  13. data/lib/kobako/catalog/snippets.rb +149 -0
  14. data/lib/kobako/catalog.rb +18 -0
  15. data/lib/kobako/codec/decoder.rb +73 -0
  16. data/lib/kobako/codec/encoder.rb +37 -0
  17. data/lib/kobako/codec/error.rb +34 -0
  18. data/lib/kobako/codec/factory.rb +162 -0
  19. data/lib/kobako/codec/utils.rb +145 -0
  20. data/lib/kobako/codec.rb +31 -0
  21. data/lib/kobako/errors.rb +140 -0
  22. data/lib/kobako/fault.rb +40 -0
  23. data/lib/kobako/handle.rb +60 -0
  24. data/lib/kobako/namespace.rb +67 -0
  25. data/lib/kobako/outcome/panic.rb +42 -0
  26. data/lib/kobako/outcome.rb +166 -0
  27. data/lib/kobako/runtime.rb +30 -0
  28. data/lib/kobako/sandbox.rb +314 -0
  29. data/lib/kobako/sandbox_options.rb +70 -0
  30. data/lib/kobako/snapshot.rb +40 -0
  31. data/lib/kobako/snippet/binary.rb +29 -0
  32. data/lib/kobako/snippet/source.rb +28 -0
  33. data/lib/kobako/snippet.rb +18 -0
  34. data/lib/kobako/transport/dispatcher.rb +195 -0
  35. data/lib/kobako/transport/error.rb +24 -0
  36. data/lib/kobako/transport/request.rb +78 -0
  37. data/lib/kobako/transport/response.rb +69 -0
  38. data/lib/kobako/transport/run.rb +141 -0
  39. data/lib/kobako/transport/yield.rb +91 -0
  40. data/lib/kobako/transport/yielder.rb +89 -0
  41. data/lib/kobako/transport.rb +24 -0
  42. data/lib/kobako/usage.rb +41 -0
  43. data/lib/kobako/version.rb +5 -0
  44. data/lib/kobako.rb +10 -0
  45. data/release-please-config.json +24 -0
  46. data/sig/kobako/capture.rbs +11 -0
  47. data/sig/kobako/catalog/handles.rbs +19 -0
  48. data/sig/kobako/catalog/namespaces.rbs +17 -0
  49. data/sig/kobako/catalog/snippets.rbs +27 -0
  50. data/sig/kobako/catalog.rbs +4 -0
  51. data/sig/kobako/codec/decoder.rbs +12 -0
  52. data/sig/kobako/codec/encoder.rbs +7 -0
  53. data/sig/kobako/codec/error.rbs +18 -0
  54. data/sig/kobako/codec/factory.rbs +31 -0
  55. data/sig/kobako/codec/utils.rbs +19 -0
  56. data/sig/kobako/errors.rbs +55 -0
  57. data/sig/kobako/fault.rbs +19 -0
  58. data/sig/kobako/handle.rbs +18 -0
  59. data/sig/kobako/namespace.rbs +19 -0
  60. data/sig/kobako/outcome/panic.rbs +34 -0
  61. data/sig/kobako/outcome.rbs +24 -0
  62. data/sig/kobako/runtime.rbs +23 -0
  63. data/sig/kobako/sandbox.rbs +55 -0
  64. data/sig/kobako/sandbox_options.rbs +32 -0
  65. data/sig/kobako/snapshot.rbs +15 -0
  66. data/sig/kobako/snippet/binary.rbs +12 -0
  67. data/sig/kobako/snippet/source.rbs +13 -0
  68. data/sig/kobako/snippet.rbs +4 -0
  69. data/sig/kobako/transport/dispatcher.rbs +34 -0
  70. data/sig/kobako/transport/error.rbs +6 -0
  71. data/sig/kobako/transport/request.rbs +32 -0
  72. data/sig/kobako/transport/response.rbs +30 -0
  73. data/sig/kobako/transport/run.rbs +27 -0
  74. data/sig/kobako/transport/yield.rbs +34 -0
  75. data/sig/kobako/transport/yielder.rbs +21 -0
  76. data/sig/kobako/transport.rbs +4 -0
  77. data/sig/kobako/usage.rbs +11 -0
  78. data/sig/kobako.rbs +3 -0
  79. metadata +145 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f8ef4754a6ada28af662bb388fdc9ab4eac1d1059994bcdaf51ea2cf4762e967
4
+ data.tar.gz: 2d0f3941b97b6c350fb87a3e425276eed154bec6fb39134535e6b194172b45a0
5
+ SHA512:
6
+ metadata.gz: 394498656116fa39938fb00d09ab43bae5c6b6f3371702a11290988f91ec859f6c316384cb1b48ccd8843a22e15ff2799be1a739f01a3d6f8737284e69ad97da
7
+ data.tar.gz: 5f4ff76c26c7cd573a42e516d10bcbd7f6e3e9abe03f02ff70d732a685f40abb30f4c0fcd996242ebd4292e81f88c82bf2fa9e7db7ceb0e76f2a62d09f04b765
@@ -0,0 +1 @@
1
+ {".":"0.5.0"}
data/CHANGELOG.md ADDED
@@ -0,0 +1,29 @@
1
+ # Changelog
2
+
3
+ ## [0.5.0](https://github.com/elct9620/kobako/compare/v0.4.0...v0.5.0) (2026-05-27)
4
+
5
+
6
+ ### Features
7
+
8
+ * **abi:** add `__kobako_yield_to_block` skeleton + host re-entry channel ([555eb4b](https://github.com/elct9620/kobako/commit/555eb4bf578c3c4397ba2c0d105c0d3ca687e23c))
9
+ * **abi:** classify RBreak via ci_break_index for B-25 / E-21 ([32668a0](https://github.com/elct9620/kobako/commit/32668a033e2f959700acadadfbc41388ed72a2dd))
10
+ * **abi:** wire `__kobako_yield_to_block` to real `mrb_yield_argv` ([35aeac8](https://github.com/elct9620/kobako/commit/35aeac8700254d1500f5be837a72c56984a7ebfa))
11
+ * **bench:** add noise-aware release gate, report mean alongside median ([0cfaebc](https://github.com/elct9620/kobako/commit/0cfaebc2afadfae81e3d00441273da70e396d7a5))
12
+ * **bench:** add yield round-trip suite as gated benchmark [#6](https://github.com/elct9620/kobako/issues/6) ([315f923](https://github.com/elct9620/kobako/commit/315f923caa89bcd8752a611525da68ae53ae092f))
13
+ * **catalog:** introduce empty Kobako::Catalog namespace ([8af8c54](https://github.com/elct9620/kobako/commit/8af8c54c72e5e5193555bcc2e86072d4a4d8176d))
14
+ * **ext:** enforce the 16 MiB single-dispatch payload cap on host boundaries ([c80e281](https://github.com/elct9620/kobako/commit/c80e281e0810640c60d93174beddd49a31c34182))
15
+ * **guest:** capture guest blocks via `n*&` argspec + LIFO BLOCK_STACK ([aa55556](https://github.com/elct9620/kobako/commit/aa55556aab23c159078d0ba0ea47ed878b26e89d))
16
+ * **rpc:** build block proxy for guest-supplied yield blocks ([b6d6cf7](https://github.com/elct9620/kobako/commit/b6d6cf7f5ca857f55aafea62631b243f688c61a6))
17
+ * **rpc:** catch/throw + frame invalidator close B-25 / B-28 / E-23 ([3b21f25](https://github.com/elct9620/kobako/commit/3b21f252fafdd2070f3953460509e24a0e643d88))
18
+ * **transport:** introduce empty Kobako::Transport namespace ([85cda26](https://github.com/elct9620/kobako/commit/85cda268000490f521424339bec1664d0b33478b))
19
+ * **wire:** add `block_given` field to Request envelope ([30e004f](https://github.com/elct9620/kobako/commit/30e004fa8f00739e68883889c5225c98cf9521fe))
20
+ * **wire:** add YieldResponse envelope codec on both sides ([4592567](https://github.com/elct9620/kobako/commit/459256784af616d70738ffd0f56c3b15244b3e7c))
21
+
22
+
23
+ ### Bug Fixes
24
+
25
+ * **bench:** restore renamed class references so rake bench runs ([76140cc](https://github.com/elct9620/kobako/commit/76140cc99922973fc305aab6ba727a832ddbe7ba))
26
+ * **ext:** GC-root the dispatch Proc via a pinning mark on Kobako::Runtime ([f31bd07](https://github.com/elct9620/kobako/commit/f31bd071201b5fed7376bd13b876f103d6c6a5d6))
27
+ * **ext:** raise SandboxError, not TrapError, when #run envelope alloc fails ([a1981fe](https://github.com/elct9620/kobako/commit/a1981fea7438090a76758147e7e84543e9d96968))
28
+ * **transport:** fill E-xx placeholder and drop BLOCK_RESEARCH citations ([816ff80](https://github.com/elct9620/kobako/commit/816ff804535196036bec01fcd980e25036211b80))
29
+ * **wasm:** reject unrepresentable guest return values instead of stringifying ([c3fd069](https://github.com/elct9620/kobako/commit/c3fd0698cb168b55502fb86065406caf9a7744e1))
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 ZhengXian Qiu
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,408 @@
1
+ # Kobako
2
+
3
+ Kobako is a Ruby gem that embeds a Wasm-isolated mruby interpreter inside your application, so you can execute untrusted Ruby scripts (LLM-generated code, user formulas, student submissions, third-party plugins) in-process without giving them access to host memory, files, network, or credentials.
4
+
5
+ The host (`wasmtime`) runs a precompiled `kobako.wasm` guest containing mruby and an RPC client. The only way a guest script can reach the outside world is through Host App-declared **Services** — named Ruby objects you explicitly inject into the sandbox.
6
+
7
+ ```
8
+ Host process Wasm guest
9
+ ┌──────────────────────┐ ┌──────────────────────┐
10
+ │ Kobako::Sandbox │ ─eval─▶ │ mruby interpreter │
11
+ │ │ ─run──▶ │ │
12
+ │ Services │ ◀──RPC─ │ KV::Lookup.call(k) │
13
+ │ KV::Lookup │ ─resp─▶ │ │
14
+ │ │ │ │
15
+ │ stdout / stderr buf │ ◀─pipe─ │ puts / warn │
16
+ │ │ │ │
17
+ │ return value │ ◀─last─ │ last expression │
18
+ └──────────────────────┘ └──────────────────────┘
19
+ trusted untrusted
20
+ ```
21
+
22
+ ## Features
23
+
24
+ | Feature | Description |
25
+ |---|---|
26
+ | In-process Wasm sandbox | No subprocess, no container. Both invocation verbs (`Sandbox#eval` for ad-hoc source, `Sandbox#run` for entrypoint dispatch) are synchronous Ruby calls. |
27
+ | Per-invocation caps | Every invocation enforces a wall-clock `timeout` (default 60 s) and a per-invocation linear-memory `memory_limit` (default 1 MiB); exhaustion raises `Kobako::TimeoutError` / `Kobako::MemoryLimitError`. |
28
+ | Capability injection via Services | Guest scripts can only call Ruby objects you explicitly `bind` under a two-level `Namespace::Member` path. |
29
+ | Preloaded snippets | `Sandbox#preload` registers source or RITE bytecode for setup-once dispatch via `Sandbox#run(:Entrypoint, *args, **kwargs)`. |
30
+ | Capability Handles | Services may return stateful host objects; the guest receives an opaque `Kobako::Handle` proxy it can use as the target of follow-up RPC calls, with no way to dereference it. `Sandbox#run` also accepts non-wire-representable Ruby objects as args and auto-wraps them into Handles, so the guest can use any host object the script needs. |
31
+ | Three-class error taxonomy | Every failure is exactly one of `TrapError`, `SandboxError`, or `ServiceError`, so you can route errors without inspecting messages. |
32
+ | Per-invocation state reset | Handles issued during one invocation are invalidated before the next; Service bindings and preloaded snippets remain. |
33
+ | Separated stdout / stderr capture | Guest writes to `$stdout` / `$stderr` are buffered per-channel (1 MiB default cap, configurable); overflow is clipped and reported by `#stdout_truncated?` / `#stderr_truncated?`. |
34
+ | Per-invocation usage readout | `Sandbox#usage` returns the most recent invocation's `wall_time` (Float seconds spent inside the wasm guest) and `memory_peak` (high-water `memory.grow` delta in bytes), populated on every outcome including `TrapError`, for budget diagnostics. |
35
+ | Curated mruby stdlib | Core extensions plus `mruby-onig-regexp` for full Onigmo `Regexp` support; no mrbgem with I/O, network, or syscall access is bundled. |
36
+
37
+ ## Requirements
38
+
39
+ - **Ruby ≥ 3.3.0**
40
+ - **Rust / Cargo** at install time — the native extension compiles from source via `rb_sys`
41
+ - **Linux** or **macOS** — Windows is not supported
42
+
43
+ The precompiled `kobako.wasm` Guest Binary ships inside the gem, so end users do **not** need a WASI toolchain. (The toolchain is only required if you build the gem from a source checkout — see [Development](#development).)
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ bundle add kobako
49
+ # or
50
+ gem install kobako
51
+ ```
52
+
53
+ ## Quick Start
54
+
55
+ ```ruby
56
+ require "kobako"
57
+
58
+ sandbox = Kobako::Sandbox.new
59
+
60
+ result = sandbox.eval(<<~RUBY)
61
+ 1 + 2
62
+ RUBY
63
+
64
+ result # => 3
65
+ sandbox.stdout # => ""
66
+ ```
67
+
68
+ The script executes inside the Wasm guest. It cannot read your filesystem, open sockets, or touch your `ENV`.
69
+
70
+ ## Injecting Services
71
+
72
+ Guest scripts reach host resources only through Services. Declare a **Namespace**, then `bind` named **Members** on it — each member can be any Ruby object that responds to the methods the guest will call.
73
+
74
+ ```ruby
75
+ sandbox = Kobako::Sandbox.new
76
+
77
+ sandbox.define(:KV).bind(:Lookup, ->(key) { redis.get(key) })
78
+ sandbox.define(:Log).bind(:Sink, ->(msg) { logger.info(msg) })
79
+
80
+ sandbox.eval(<<~RUBY)
81
+ Log::Sink.call("starting")
82
+ KV::Lookup.call("user_42")
83
+ RUBY
84
+ # => "..." (the redis value)
85
+ ```
86
+
87
+ Names must match the Ruby constant pattern `/\A[A-Z]\w*\z/`. Services declared before the first invocation remain active across subsequent invocations; `define` after the first invocation (`#eval` or `#run`) raises `ArgumentError`.
88
+
89
+ ### Keyword arguments
90
+
91
+ Keyword keys travel as Symbols and reach the host method as keyword arguments:
92
+
93
+ ```ruby
94
+ sandbox.define(:Geo).bind(:Lookup, ->(name:, region:) { "#{region}/#{name}" })
95
+
96
+ sandbox.eval('Geo::Lookup.call(name: "alice", region: "us")')
97
+ # => "us/alice"
98
+ ```
99
+
100
+ ## Per-invocation caps
101
+
102
+ Each Sandbox enforces a wall-clock timeout and a guest linear-memory cap on every invocation (`#eval` or `#run`). Both default to safe values; pass `nil` to `timeout` or `memory_limit` to disable that cap. The output caps (`stdout_limit` / `stderr_limit`) cannot be disabled — pass a large Integer instead.
103
+
104
+ ```ruby
105
+ sandbox = Kobako::Sandbox.new(
106
+ timeout: 5.0, # seconds, default 60.0
107
+ memory_limit: 10 * 1024 * 1024, # bytes, default 1 MiB
108
+ stdout_limit: 64 * 1024, # bytes, default 1 MiB
109
+ stderr_limit: 64 * 1024
110
+ )
111
+ ```
112
+
113
+ | Cap | Raises (subclass of `TrapError`) | Default |
114
+ |----------------|------------------------------------|----------|
115
+ | `timeout` | `Kobako::TimeoutError` | 60.0 s |
116
+ | `memory_limit` | `Kobako::MemoryLimitError` | 1 MiB |
117
+ | `stdout_limit` | output silently clipped at cap | 1 MiB |
118
+ | `stderr_limit` | output silently clipped at cap | 1 MiB |
119
+
120
+ The timeout deadline is absolute wall-clock from invocation entry and is checked at guest Wasm safepoints. Long-running host Service callbacks still consume wall-clock time but do not themselves trap — the next guest safepoint will trap immediately on return if the deadline has passed.
121
+
122
+ `memory_limit` is scoped to the **per-invocation linear-memory delta** — the budget covers how much the current `#eval` / `#run` may grow `memory.grow` past the size observed at invocation entry. The mruby image's initial allocation and prior invocations' high-water mark are folded into that entry baseline, so a Sandbox reused across many invocations does not silently accumulate against a global budget.
123
+
124
+ The 1 MiB default targets lightweight dynamic RPC workloads — short scripts that orchestrate Service calls, return small structured values, or replace a tool-calling layer in an AI Agent's Code Mode dispatch. Bump `memory_limit` when scripts compose multi-hundred-KiB strings, hold large composite return values, or run computations that allocate substantial intermediate state. Because the cap resets every invocation, multi-call patterns on one Sandbox do not need a budget that covers their cumulative footprint — only the largest single invocation's working set.
125
+
126
+ To see how much of the cap an invocation actually consumed, read `Sandbox#usage` after the call. It returns a `Kobako::Usage` value object with `wall_time` (Float seconds the guest export call spent inside wasmtime, aligned with the `timeout` accounting) and `memory_peak` (Integer high-water `memory.grow` delta in bytes, aligned with the `memory_limit` accounting). The fields are populated on every outcome, including the `TrapError` branches, so you can read them after rescuing a trap to diagnose which budget the failing invocation chewed through.
127
+
128
+ ```ruby
129
+ sandbox = Kobako::Sandbox.new(timeout: 1.0, memory_limit: 4 * 1024 * 1024)
130
+
131
+ begin
132
+ sandbox.eval("'x' * 5_000_000")
133
+ rescue Kobako::MemoryLimitError
134
+ sandbox.usage.memory_peak # => the largest delta accepted before the trap
135
+ sandbox.usage.wall_time # => seconds spent before the cap fired
136
+ end
137
+ ```
138
+
139
+ ## Capturing stdout and stderr
140
+
141
+ Guest output is captured into per-invocation buffers and exposed independently from the return value. The buffers cover the full Ruby IO surface — `puts`, `print`, `printf`, `p`, `<<`, and writes through `$stdout` / `$stderr` — all routed through the host-captured WASI pipe.
142
+
143
+ ```ruby
144
+ sandbox = Kobako::Sandbox.new
145
+
146
+ result = sandbox.eval(<<~RUBY)
147
+ puts "hello"
148
+ warn "be careful"
149
+ 42
150
+ RUBY
151
+
152
+ result # => 42
153
+ sandbox.stdout # => "hello\n"
154
+ sandbox.stderr # => "be careful\n"
155
+ ```
156
+
157
+ Each invocation clears the buffers at start. Output past the per-channel cap is clipped at the cap boundary — the invocation still returns normally, the bytes carry no truncation sentinel, and `#stdout_truncated?` / `#stderr_truncated?` flip to `true`.
158
+
159
+ ```ruby
160
+ sandbox = Kobako::Sandbox.new(stdout_limit: 64 * 1024)
161
+ sandbox.eval('puts "a" * 100_000')
162
+ sandbox.stdout.bytesize # => 65_536
163
+ sandbox.stdout_truncated? # => true
164
+ ```
165
+
166
+ ## Error handling
167
+
168
+ Every invocation (`#eval` or `#run`) either returns a value or raises exactly one of three classes:
169
+
170
+ ```ruby
171
+ begin
172
+ sandbox.eval(script)
173
+ rescue Kobako::TrapError => e
174
+ # Wasm engine fault OR per-invocation cap exhaustion:
175
+ # - Kobako::TimeoutError (wall-clock timeout)
176
+ # - Kobako::MemoryLimitError (memory_limit exceeded)
177
+ # - Kobako::TrapError (engine crash / wire-violation fallback)
178
+ # The Sandbox is unrecoverable — discard and recreate it.
179
+ rescue Kobako::ServiceError => e
180
+ # A Service call failed and the script did not rescue it.
181
+ # Treat like any other downstream-service failure in your app.
182
+ rescue Kobako::SandboxError => e
183
+ # The script itself raised, failed to compile, or produced an
184
+ # unrepresentable value. A script-level fault, not infrastructure.
185
+ end
186
+ ```
187
+
188
+ `SandboxError` and `ServiceError` carry structured fields (`origin`, `klass`, `backtrace_lines`, `details`) when the guest produced a panic envelope. Named subclasses:
189
+
190
+ | Class | Parent | Trigger |
191
+ |----------------------------------------|--------------------|------------------------------------------------------------------------------------------|
192
+ | `Kobako::TimeoutError` | `TrapError` | Per-invocation `timeout` exhausted |
193
+ | `Kobako::MemoryLimitError` | `TrapError` | Per-invocation `memory_limit` exhausted |
194
+ | `Kobako::HandleTableExhausted` | `SandboxError` | Per-invocation Handle counter reached its 2³¹ − 1 cap |
195
+ | `Kobako::BytecodeError` | `SandboxError` | `#preload(binary:)` payload failed RITE structural validation at first invocation replay |
196
+
197
+ ## Capability Handles
198
+
199
+ When a Service returns a stateful host object (anything beyond `nil` / Boolean / Integer / Float / String / Symbol / Array / Hash), the wire layer transparently allocates an opaque Handle. The guest receives a `Kobako::Handle` proxy it can use as the target of further RPC calls — but cannot dereference, forge from an integer, or smuggle across runs.
200
+
201
+ ```ruby
202
+ class Greeter
203
+ def initialize(name) = @name = name
204
+ def greet = "hi, #{@name}"
205
+ end
206
+
207
+ sandbox.define(:Factory).bind(:Make, ->(name) { Greeter.new(name) })
208
+
209
+ sandbox.eval(<<~RUBY)
210
+ g = Factory::Make.call("Bob") # g is a Kobako::Handle proxy
211
+ g.greet # second RPC, routed to the Greeter
212
+ RUBY
213
+ # => "hi, Bob"
214
+ ```
215
+
216
+ `Sandbox#run` accepts non-wire-representable host objects as args / kwargs values too: the host walks the argument tree, wraps every non-wire leaf through the same Handle path, and the guest sees a `Kobako::Handle` proxy in its place. This lets you pass framework objects (a Rack `env` Hash containing an `IO`-like body, an active record, an enumerator) into the entrypoint without first marshalling them into primitives.
217
+
218
+ ```ruby
219
+ require "stringio"
220
+
221
+ sandbox = Kobako::Sandbox.new
222
+ sandbox.preload(code: "Echo = ->(body) { body.read.upcase }", name: :Echo)
223
+
224
+ sandbox.run(:Echo, StringIO.new("hello world"))
225
+ # => "HELLO WORLD"
226
+ ```
227
+
228
+ Handles are scoped to a single invocation — a Handle obtained in invocation N is invalid in invocation N+1, even on the same Sandbox.
229
+
230
+ ## Setup-once, run-many
231
+
232
+ A single Sandbox can serve many invocations. Service bindings and preloaded snippets persist; capability state (Handles, stdout, stderr) resets between invocations.
233
+
234
+ ```
235
+ ───────────── setup phase (mutable) ─────────────
236
+
237
+ sandbox = Kobako::Sandbox.new
238
+ sandbox.define(:KV).bind(:Lookup, ...)
239
+ sandbox.preload(code: ..., name: :Adder)
240
+ sandbox.preload(code: ..., name: :Greeter)
241
+
242
+
243
+
244
+
245
+ ═════════════════ seal point ═════════════════
246
+ First #eval or #run freezes the Service registry
247
+ and snippet table. Further define / preload now
248
+ raise ArgumentError.
249
+
250
+
251
+
252
+
253
+ ──────────────── invocation N ───────────────────
254
+
255
+ 1. allocate fresh mrb_state
256
+
257
+ 2. replay snippets (in insertion order):
258
+ :Adder → defines Adder
259
+ :Greeter → defines Greeter
260
+
261
+ 3. dispatch: eval(source) or run(:Target, *args)
262
+
263
+ 4. return value to host
264
+
265
+ 5. discard mrb_state; reset per-invocation state:
266
+ · Handles invalidated
267
+ · stdout / stderr buffers cleared
268
+ · memory delta zeroed
269
+
270
+ Services + snippets persist; invocation N+1 repeats.
271
+ ```
272
+
273
+ ```ruby
274
+ sandbox = Kobako::Sandbox.new
275
+ sandbox.define(:Data).bind(:Fetch, ->(id) { records[id] })
276
+
277
+ sandbox.eval('Data::Fetch.call("a")') # => "..."
278
+ sandbox.eval('Data::Fetch.call("b")') # => "..." (same bindings, fresh state)
279
+ ```
280
+
281
+ For workloads that must be isolated from each other (e.g., one Sandbox per tenant, per student submission), construct a fresh `Kobako::Sandbox` per scope. wasmtime's Engine and the compiled Module are cached at process scope, so additional Sandboxes amortize cold-start cost automatically.
282
+
283
+ ## Preloaded snippets and entrypoint dispatch
284
+
285
+ `Sandbox#preload` registers named mruby snippets that replay against the fresh `mrb_state` before every invocation; `Sandbox#run(:Target, *args, **kwargs)` dispatches into a top-level `Object` constant defined by those snippets and returns the value of `Target.call(*args, **kwargs)`. Together they cover setup-once / dispatch-many workloads where the same logic is exercised across many requests.
286
+
287
+ ```ruby
288
+ sandbox = Kobako::Sandbox.new
289
+ sandbox.preload(code: "Adder = ->(a, b) { a + b }", name: :Adder)
290
+ sandbox.preload(code: 'Greeter = ->(name:) { "hello, #{name}" }', name: :Greeter)
291
+
292
+ sandbox.run(:Adder, 2, 3) # => 5
293
+ sandbox.run(:Greeter, name: "world") # => "hello, world"
294
+ ```
295
+
296
+ `#preload` accepts two payload forms:
297
+
298
+ | Form | Signature | Snippet name source | Validation timing |
299
+ |----------|----------------------------------------|-------------------------------------|------------------------------------------------------------------------------------------|
300
+ | Source | `preload(code: "...", name: :Const)` | The `name:` keyword | Trial-compiled at preload time; compile errors raise immediately |
301
+ | Bytecode | `preload(binary: bytes)` | Read from the bytecode's `debug_info` | Structural validation runs at first invocation; failure raises `Kobako::BytecodeError` |
302
+
303
+ The source form trial-compiles each snippet against a fresh `mrb_state` at preload time, so compile errors surface immediately at the `#preload` call. The bytecode form treats `binary:` as opaque bytes and defers RITE version / body validation to the first invocation's replay, because that is when the payload loads into a fresh `mrb_state`. Bytecode compiled without `debug_info` (`mrbc` without `-g`) is still accepted — only its backtrace frames are omitted, while exception class, message, and `origin` attribution are preserved.
304
+
305
+ Snippets replay in insertion order, so later snippets can reference constants defined by earlier ones. The snippet table is sealed by the first invocation alongside Service registration; additional `#preload` calls after the first `#eval` or `#run` raise `ArgumentError`.
306
+
307
+ ```
308
+ per-invocation replay (every #eval / #run, snippets in insertion order):
309
+
310
+ fresh mrb_state
311
+
312
+ ├──▶ replay :Adder (defines Adder)
313
+
314
+ ├──▶ replay :Greeter (defines Greeter)
315
+
316
+ └──▶ eval(source) -or- run(:Target, *args, **kwargs)
317
+
318
+
319
+ return value, then mrb_state discarded
320
+ ```
321
+
322
+ `#run` resolves `target` (Symbol or String, normalized to Symbol) only as a top-level `Object` constant — `::`-segmented names and lowercase forms fail at host pre-flight with `ArgumentError`. A `Kobako::SandboxError` surfaces when the constant is missing or does not respond to `#call`.
323
+
324
+ ### Choosing between source and bytecode
325
+
326
+ Use the **source form** when snippets are authored in your repo or generated at boot — compile errors land at the `#preload` call so a misbehaving snippet fails fast at setup time, and no separate `mrbc` toolchain is needed. The trial-compile happens once per snippet (~2.5 µs per snippet) and is paid at preload, not on the request hot path.
327
+
328
+ Use the **bytecode form** when snippets ship as build artifacts from a pipeline that runs `mrbc` separately — for example, when source bodies should not be embedded in the running process, when you want a build step that compiles and packages snippets ahead of release, or when you want `Exception#backtrace` frames attributed to the bytecode's `debug_info` filename rather than a host-supplied `name:` keyword. Structural validation (RITE version, body integrity) is deferred to the first invocation, so a malformed bytecode payload surfaces as `Kobako::BytecodeError` on the first `#eval` or `#run`, not at `#preload`.
329
+
330
+ Both forms behave identically at dispatch time and replay through the same per-invocation path, so the choice between them is about your build / distribution pipeline and where you want errors to land, not about runtime cost.
331
+
332
+ ## Performance
333
+
334
+ Order-of-magnitude figures for capacity planning on macOS arm64, Ruby 3.4.7, YJIT off. Absolute values vary by hardware but the ratios are stable across machines. Detailed numbers and methodology live in [`benchmark/README.md`](benchmark/README.md).
335
+
336
+ ### Lifecycle costs
337
+
338
+ | Phase | Cost |
339
+ |-------------------------------------------------------------|-------------------------------------------------|
340
+ | First `Sandbox.new` in a fresh process (Engine + Module JIT) | ~600 ms one-time |
341
+ | Subsequent `Sandbox.new` (Engine cache warm) | ~130 µs |
342
+ | Reusing a Sandbox for one `#eval("nil")` | ~135 µs |
343
+ | Fresh `Sandbox.new` per request | ~275 µs (≈ +140 µs vs reuse) |
344
+ | Warm `#run(:Entrypoint, ...)` dispatch | ~165 µs |
345
+ | Per-RPC cost amortized inside one invocation | ~6.6 µs (1 000 RPCs in one `#eval` ≈ 6.6 ms) |
346
+ | 100 000-iteration integer XOR loop in mruby | ~43 ms |
347
+ | 1 000 Onigmo `Regexp =~` matches | ~3 µs each |
348
+
349
+ The ~600 ms cold start dominates the first Sandbox in a process — wasmtime JIT-compiles the precompiled `kobako.wasm` Module and the result is cached at process scope. Construct one Sandbox at boot before serving requests so the JIT cost lands off the hot path.
350
+
351
+ ### Memory budget
352
+
353
+ | Allocation | Cost |
354
+ |---------------------------------------------|----------------------------------------------------------------------------|
355
+ | Process RSS after first `Sandbox.new` | ~165-195 MB (one-time engine + module + first instance) |
356
+ | Per additional Sandbox | ~580 KB (Wasm instance + linear memory + WASI capture pipes) |
357
+ | 1 000 isolated tenants in one process | ~765 MB total |
358
+
359
+ Use these as upper-bound budgets for capacity planning, not lower bounds — actual RSS shifts ~30% with host process load and macOS allocator state.
360
+
361
+ ### Choosing your pattern
362
+
363
+ When the script is ad-hoc (LLM-generated, untrusted user input) and only runs once, use `Sandbox#eval(source)`. Per-invocation cost is ~135 µs of setup plus the script's own runtime; mruby parses the source on every call.
364
+
365
+ When you have a fixed set of entrypoints exercised many times — a stable AI Agent tool-call protocol, a plug-in registry loaded at boot, a small library of host-side commands — preload the entrypoints via `Sandbox#preload(code:, name:)` once at setup and dispatch via `Sandbox#run(:Target, *args, **kwargs)`. The mruby source compile (~2.5 µs per snippet) lands once at preload, not on every request, and warm dispatch costs ~165 µs.
366
+
367
+ Mind the snippet replay cost. Every preloaded snippet replays into a fresh `mrb_state` before **every** invocation, whether the invocation is `#eval` or `#run`, at ~7-9 µs per snippet per invocation. Preloading 8 helpers adds ~60 µs to every subsequent invocation; preloading 64 helpers adds ~565 µs. Keep the snippet count proportionate to how often the helpers are actually used — preloading rarely-touched helpers is more expensive than inlining or re-eval'ing them.
368
+
369
+ For tenant isolation between mutually untrusted scopes, construct a fresh `Kobako::Sandbox` per scope. Per-request construction costs ~140 µs over reuse plus ~580 KB of RSS — comfortably affordable for 1 000+ isolated tenants in one Sidekiq / Puma worker. Reuse a Sandbox when all requests share one trust scope; isolate when scripts come from many.
370
+
371
+ ### Concurrency
372
+
373
+ `ext/` does not release the GVL during wasmtime execution, so wasm work is GVL-serialized: aggregate throughput across N Threads stays around 7-8k `#eval`/s regardless of N. Ruby-side `#eval` setup can still overlap, so a short `#eval` running while another Thread is in a long `#eval` is slowed by ~2× (not 10×) — host-side synchronization yields the GVL and the contending Thread interleaves. Mixed short / long workloads in one process do not deadlock.
374
+
375
+ ### Regression gate
376
+
377
+ A +10% regression on any of the five SPEC-mandated benchmarks (cold_start, RPC roundtrip, codec, mruby VM, HandleTable) blocks release. Full per-suite breakdown in [`benchmark/README.md`](benchmark/README.md).
378
+
379
+ ```bash
380
+ bundle exec rake bench # five gated regression benchmarks (~5-8 min, ≤ 1 MiB payloads)
381
+ ```
382
+
383
+ ## Development
384
+
385
+ After checking out the repo:
386
+
387
+ ```bash
388
+ bin/setup # install dependencies
389
+ bundle exec rake # default: compile + test + rubocop + steep
390
+ ```
391
+
392
+ Building from source requires a WASI-capable Rust toolchain in addition to the standard host toolchain. The first compile walks the full vendor / mruby / wasm chain:
393
+
394
+ ```bash
395
+ bundle exec rake compile # build the native extension
396
+ bundle exec rake wasm:build # rebuild data/kobako.wasm
397
+ bundle exec rake test # run the Ruby test suite
398
+ ```
399
+
400
+ `bin/console` opens an IRB session with the gem preloaded for experimentation. To install the local checkout as a gem, run `bundle exec rake install`.
401
+
402
+ ## Contributing
403
+
404
+ Bug reports and pull requests are welcome at <https://github.com/elct9620/kobako>. Please open an issue before starting on non-trivial changes so we can align on scope.
405
+
406
+ ## License
407
+
408
+ Kobako is released under the [Apache License 2.0](https://opensource.org/licenses/Apache-2.0).
data/data/kobako.wasm ADDED
Binary file
Binary file