kobako 0.26.0-aarch64-linux → 0.28.0-aarch64-linux

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 (81) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +103 -0
  4. data/README.md +156 -118
  5. data/ROADMAP.md +23 -22
  6. data/SECURITY.md +1 -1
  7. data/crates/kobako-runtime/CHANGELOG.md +38 -0
  8. data/crates/kobako-runtime/README.md +10 -12
  9. data/crates/kobako-transport/CHANGELOG.md +35 -0
  10. data/crates/kobako-transport/README.md +43 -27
  11. data/crates/kobako-wasmtime/CHANGELOG.md +51 -0
  12. data/crates/kobako-wasmtime/README.md +8 -10
  13. data/data/kobako.wasm +0 -0
  14. data/lib/kobako/3.3/kobako.so +0 -0
  15. data/lib/kobako/3.4/kobako.so +0 -0
  16. data/lib/kobako/4.0/kobako.so +0 -0
  17. data/lib/kobako/capture.rb +1 -4
  18. data/lib/kobako/catalog/extensions.rb +21 -59
  19. data/lib/kobako/catalog/flags.rb +41 -0
  20. data/lib/kobako/catalog/handles.rb +7 -54
  21. data/lib/kobako/catalog/services.rb +7 -61
  22. data/lib/kobako/catalog/snippets.rb +42 -66
  23. data/lib/kobako/catalog.rb +2 -10
  24. data/lib/kobako/codec/decoder.rb +9 -28
  25. data/lib/kobako/codec/encoder.rb +3 -22
  26. data/lib/kobako/codec/error.rb +6 -27
  27. data/lib/kobako/codec/ext_types.rb +72 -94
  28. data/lib/kobako/codec/handle_walk.rb +65 -130
  29. data/lib/kobako/codec/nesting.rb +3 -8
  30. data/lib/kobako/codec/state.rb +4 -10
  31. data/lib/kobako/codec/utils.rb +6 -34
  32. data/lib/kobako/codec.rb +3 -22
  33. data/lib/kobako/constant_name.rb +8 -0
  34. data/lib/kobako/context.rb +51 -112
  35. data/lib/kobako/errors.rb +51 -127
  36. data/lib/kobako/execution.rb +16 -21
  37. data/lib/kobako/extension.rb +23 -40
  38. data/lib/kobako/handle.rb +7 -12
  39. data/lib/kobako/outcome.rb +51 -80
  40. data/lib/kobako/payload/arguments.rb +1 -16
  41. data/lib/kobako/payload.rb +4 -12
  42. data/lib/kobako/pool.rb +23 -53
  43. data/lib/kobako/runtime.rb +7 -21
  44. data/lib/kobako/sandbox.rb +96 -133
  45. data/lib/kobako/sandbox_options.rb +38 -84
  46. data/lib/kobako/snippet/binary.rb +1 -16
  47. data/lib/kobako/snippet/source.rb +1 -16
  48. data/lib/kobako/snippet.rb +1 -9
  49. data/lib/kobako/transport/call.rb +1 -14
  50. data/lib/kobako/transport/dispatcher.rb +112 -195
  51. data/lib/kobako/transport/error.rb +6 -20
  52. data/lib/kobako/transport/exposure.rb +5 -10
  53. data/lib/kobako/transport/reflection.rb +39 -50
  54. data/lib/kobako/transport/run.rb +17 -71
  55. data/lib/kobako/transport/yielder.rb +21 -73
  56. data/lib/kobako/transport.rb +3 -15
  57. data/lib/kobako/unresolved.rb +4 -11
  58. data/lib/kobako/usage.rb +2 -6
  59. data/lib/kobako/version.rb +1 -1
  60. data/sig/kobako/catalog/extensions.rbs +2 -2
  61. data/sig/kobako/catalog/flags.rbs +17 -0
  62. data/sig/kobako/catalog/services.rbs +0 -6
  63. data/sig/kobako/catalog/snippets.rbs +4 -4
  64. data/sig/kobako/codec/ext_types.rbs +10 -8
  65. data/sig/kobako/codec/handle_walk.rbs +9 -7
  66. data/sig/kobako/codec/utils.rbs +2 -2
  67. data/sig/kobako/codec.rbs +2 -2
  68. data/sig/kobako/constant_name.rbs +3 -0
  69. data/sig/kobako/context.rbs +2 -3
  70. data/sig/kobako/errors.rbs +6 -0
  71. data/sig/kobako/outcome.rbs +6 -8
  72. data/sig/kobako/runtime.rbs +12 -8
  73. data/sig/kobako/sandbox.rbs +4 -0
  74. data/sig/kobako/sandbox_options.rbs +2 -6
  75. data/sig/kobako/transport/dispatcher.rbs +14 -18
  76. data/sig/kobako/transport/reflection.rbs +5 -3
  77. data/sig/kobako/transport/run.rbs +1 -3
  78. data/sig/kobako/transport/yielder.rbs +3 -3
  79. data/sig/kobako/transport.rbs +7 -6
  80. metadata +6 -3
  81. data/rust-toolchain.toml +0 -11
data/lib/kobako/errors.rb CHANGED
@@ -1,103 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Top-level Kobako namespace.
4
3
  module Kobako
5
- # Error taxonomy.
6
- #
7
- # Every +Kobako::Sandbox+ invocation (+#eval+ or +#run+) either returns a
8
- # value or raises exactly one of TrapError, SandboxError, or ServiceError.
9
- # Attribution is decided after the guest binary returns control to the
10
- # host: first the Wasm-trap layer, then the outcome-envelope tag.
11
- # SetupError and PoolTimeoutError sit outside the invocation taxonomy and
12
- # never pass through that attribution decision. Each class below
13
- # documents its own layer.
14
-
15
- # Base for all kobako-raised errors so callers that want to ignore the
16
- # taxonomy can rescue a single class.
4
+ # Every Sandbox#eval or Sandbox#run returns, or raises exactly one of
5
+ # TrapError, SandboxError, or ServiceError. SetupError and PoolTimeoutError
6
+ # arise outside any invocation.
7
+
8
+ # The base of every error kobako raises.
17
9
  class Error < StandardError; end
18
10
 
19
- # Carries the frozen +Kobako::Execution+ of the run that failed, so a
20
- # rescue reads its captures and usage exactly as a successful caller reads
21
- # them off the return value. Mixed only into the three invocation-outcome
22
- # classes (+TrapError+ / +SandboxError+ / +ServiceError+ and their
23
- # subclasses); +SetupError+ and +PoolTimeoutError+ arise outside any
24
- # invocation and carry no Execution. Defaults to +nil+ — a pre-flight
25
- # failure that ran no invocation leaves it unset.
11
+ # Gives an invocation failure the Execution of the run that failed, so a
12
+ # rescue reads its captures and usage as a successful caller would.
13
+ # +execution+ is +nil+ when the failure came before the guest ran.
26
14
  module CarriesExecution
15
+ # The Execution of the run that failed.
27
16
  attr_reader :execution
28
17
 
29
- # Attach the failed run's +Kobako::Execution+ and return +self+ so the
30
- # raise site reads +raise error.with_execution(execution)+.
31
- def with_execution(execution)
18
+ def with_execution(execution) # :nodoc:
32
19
  @execution = execution
33
20
  self
34
21
  end
35
22
  end
36
23
 
37
- # Wasm engine layer. Raised when the Wasm execution engine crashed
38
- # (trap, OOM, unreachable) or when the wire layer detected a structural
39
- # violation that signals a corrupted guest execution environment (an
40
- # outcome the core envelope cannot frame, an absent one included).
41
- #
42
- # Two named subclasses cover the configured per-invocation caps:
43
- #
44
- # * TimeoutError — wall-clock +timeout+ exceeded.
45
- # * MemoryLimitError — guest +memory.grow+ would exceed
46
- # +memory_limit+.
47
- #
48
- # Host Apps that only care about "guest is unrecoverable, discard the
49
- # Sandbox" can rescue +TrapError+ and ignore the subclass; Host Apps that
50
- # want to surface a specific reason to operators can rescue the subclass
51
- # first.
24
+ # The engine stopped the guest: a trap, a cap reached, or an outcome that
25
+ # could not be read. The next invocation on the same Sandbox runs as usual.
52
26
  class TrapError < Error
53
27
  include CarriesExecution
54
28
  end
55
29
 
56
- # Wall-clock timeout cap exhausted: the absolute deadline
57
- # +entry_time + timeout+ passed and the next guest wasm safepoint
58
- # trapped. The Sandbox is unrecoverable after this point; discard and
59
- # recreate before another execution.
30
+ # The invocation ran past its +timeout+.
60
31
  class TimeoutError < TrapError; end
61
32
 
62
- # Linear-memory cap exhausted: a guest +memory.grow+ would have pushed
63
- # linear memory past the configured +memory_limit+. The Sandbox is
64
- # unrecoverable after this point; discard and recreate before another
65
- # execution.
33
+ # The invocation grew guest memory past its +memory_limit+.
66
34
  class MemoryLimitError < TrapError; end
67
35
 
68
- # Construction-layer error raised by +Kobako::Sandbox.new+ /
69
- # +Kobako::Runtime.from_path+ when the wasm runtime cannot be built
70
- # from the configured +wasm_path+ before any invocation runs —
71
- # an unreadable artifact, bytes that are not a valid Wasm module, or
72
- # engine / linker / instantiation setup failure. Construction
73
- # is not an invocation, so +SetupError+ sits beside the invocation
74
- # taxonomy under +Kobako::Error+ rather than under +TrapError+: no
75
- # Sandbox is produced, so the +TrapError+ "discard and recreate"
76
- # recovery contract does not apply.
36
+ # A Sandbox could not be built: the Guest Binary is unreadable or not a
37
+ # valid module, the runtime failed to start, or it provides less isolation
38
+ # than +profile+ asks for.
77
39
  class SetupError < Error; end
78
40
 
79
- # The named +SetupError+ subclass for the common, actionable case:
80
- # the Guest Binary artifact is absent at +wasm_path+ — the pre-build
81
- # state on a fresh clone before +bundle exec rake compile+. Host Apps
82
- # that only need "the Sandbox could not be set up" rescue +SetupError+;
83
- # those wanting to special-case the unbuilt-artifact state rescue
84
- # +ModuleNotBuiltError+ first.
41
+ # The Guest Binary is missing at +wasm_path+.
85
42
  class ModuleNotBuiltError < SetupError; end
86
43
 
87
- # The structured attribution the two invocation-failure classes carry
88
- # from a decoded guest exception — its +origin+, original +klass+, and
89
- # +backtrace_lines+ — so a Host App can inspect a failure beyond its
90
- # message. Mixed into both rather than promoted to a shared superclass
91
- # because +SandboxError+ and +ServiceError+ sit in distinct branches of
92
- # the invocation-outcome taxonomy under +Kobako::Error+.
93
- #
94
- # Data specific to one kind of failure rides a named reader on the
95
- # subclass that failure raises, the way Ruby pairs NameError#name with
96
- # +NameError#local_variables+ — see UndefinedEntrypointError.
44
+ # What the guest reported about a failure.
97
45
  module Diagnosable
46
+ # Where the failure came from (<tt>"sandbox"</tt> or <tt>"service"</tt>),
47
+ # the name of the class that raised it, and its backtrace.
98
48
  attr_reader :origin, :klass, :backtrace_lines
99
49
 
100
- def initialize(message, origin: nil, klass: nil, backtrace_lines: nil)
50
+ def initialize(message, origin: nil, klass: nil, backtrace_lines: nil) # :nodoc:
101
51
  super(message)
102
52
  @origin = origin
103
53
  @klass = klass
@@ -105,93 +55,67 @@ module Kobako
105
55
  end
106
56
  end
107
57
 
108
- # Sandbox / wire layer. Raised when the guest ran to completion but
109
- # execution failed due to a mruby script error, a protocol fault, or a
110
- # payload the host's codec could not decode out of a well-framed
111
- # outcome.
58
+ # The guest failed: the script raised or did not compile, the Guest Binary
59
+ # could not start the run, or what it produced could not be read.
112
60
  class SandboxError < Error
113
61
  include Diagnosable
114
62
  include CarriesExecution
115
63
  end
116
64
 
117
- # Service layer. Raised when a Service capability call inside a mruby
118
- # script reported an application-level failure that the script did not
119
- # rescue. The base class covers a Service that ran and raised; the two
120
- # subclasses below cover the calls that never reached one, so a Host App
121
- # routes them apart with +rescue+ instead of by reading the message.
65
+ # A Service call failed and the guest left it unrescued.
122
66
  class ServiceError < Error
123
67
  include Diagnosable
124
68
  include CarriesExecution
125
69
  end
126
70
 
127
- # The ServiceError subclass raised when the call reached no Service
128
- # method: the bound path holds nothing, the Capability Handle is not
129
- # live in this invocation, or the method is absent or outside the guest
130
- # surface. The causes stay indistinguishable — an opaque target must
131
- # disclose nothing about which methods it defines — so what a Host App
132
- # learns is that this call will not succeed by being retried.
71
+ # The call reached no Service method: nothing is bound there, the Handle
72
+ # is not live, or the guest may not call that method. The causes look the
73
+ # same on purpose, so a target discloses nothing about what it defines.
133
74
  class NoServiceError < ServiceError; end
134
75
 
135
- # The ServiceError subclass raised when the call reached the Service
136
- # method but its arguments did not fit — an unknown keyword, or an
137
- # arity mismatch.
76
+ # The call reached the Service method with arguments that did not fit.
138
77
  class ServiceArgumentError < ServiceError; end
139
78
 
140
- # Raised at a Service method's +yield+ site when the guest block it
141
- # yielded to raised. A Service rescues it the way it would rescue a
142
- # block's exception without a Sandbox between the two frames; leaving it
143
- # unrescued returns it to the guest, which re-raises the exception it
144
- # raised in the first place. It therefore never reaches the Host App as
145
- # an invocation outcome and carries no Execution. +klass+ names the
146
- # guest-side class, which has no host counterpart to rebuild.
79
+ # Raised at a Service's +yield+ when the guest block raised; rescue it as
80
+ # you would the block's own exception. Left unrescued, the guest sees its
81
+ # own exception again, so it never reaches the Host App.
147
82
  class BlockError < Error
148
83
  include Diagnosable
149
84
  end
150
85
 
151
- # Raised at a Service method's +yield+ site when a yield argument has no
152
- # wire representation, so the round-trip never left the host and the
153
- # block never ran. As BlockError, a Service rescues it at that site and
154
- # may go on, so it never reaches the Host App as an invocation outcome;
155
- # unrescued, the dispatch answers as that Service having failed.
86
+ # Raised at a Service's +yield+ when an argument cannot cross to the guest,
87
+ # so the block never ran. Left unrescued, the Service call fails.
156
88
  class YieldValueError < Error; end
157
89
 
158
- # HandleExhaustedError is the canonical SandboxError subclass for the
159
- # id-cap-hit path. Raised when the per-invocation Handle ID counter in
160
- # Catalog::Handles reaches +0x7fff_ffff+ (2³¹ − 1) and further
161
- # allocation would exceed the cap.
90
+ # Raised at a Service's +yield+ when the guest serves no block, so the
91
+ # block never ran. Left unrescued, the Service call fails.
92
+ class UnsupportedError < Error; end
93
+
94
+ # One invocation issued more Handles than an id can number (2**31 - 1).
162
95
  class HandleExhaustedError < SandboxError; end
163
96
 
164
- # BytecodeError is the SandboxError subclass raised when a
165
- # +#preload(binary:)+ snippet fails structural validation during the
166
- # first invocation's snippet replay against a fresh +mrb_state+ (RITE
167
- # version mismatch or corrupt body). Bytecode that loads cleanly and
168
- # then raises at top level surfaces as plain +SandboxError+ with the
169
- # natural mruby class preserved. Inherits from SandboxError so a single
170
- # +rescue Kobako::SandboxError+ covers both source and bytecode
171
- # snippet failures while callers wanting bytecode-specific handling
172
- # can +rescue Kobako::BytecodeError+ directly.
97
+ # A <tt>preload(binary:)</tt> snippet failed to load. Bytecode that loads
98
+ # and then raises is a plain SandboxError.
173
99
  class BytecodeError < SandboxError; end
174
100
 
175
- # UndefinedEntrypointError is the SandboxError subclass raised when a
176
- # +#run+ target names no top-level constant in the guest. It carries the
177
- # +#name+ that was asked for and the +#available+ names it could have
178
- # been — the top-level constants the preloaded snippets contributed — so
179
- # a caller corrects the name from the error rather than by reading the
180
- # guest source. Ruby pairs NameError#name with
181
- # +NameError#local_variables+ for the same reason.
101
+ # The Guest Binary does not support a flag the Sandbox set, so no script
102
+ # ran. Retrying without the flag, or on a Guest Binary that supports it,
103
+ # can succeed.
104
+ class UnsupportedFlagError < SandboxError; end
105
+
106
+ # Sandbox#run named no top-level constant.
182
107
  class UndefinedEntrypointError < SandboxError
108
+ # The constant asked for, and the ones the preloaded snippets define.
183
109
  attr_reader :name, :available
184
110
 
185
- def initialize(message, name: nil, available: [], **)
111
+ def initialize(message, name: nil, available: [], **) # :nodoc:
186
112
  super(message, **)
187
113
  @name = name
188
114
  @available = available
189
115
  end
190
116
  end
191
117
 
192
- # Pool checkout layer. Raised by +Kobako::Pool#with+ when the checkout
193
- # wait exceeded the configured +checkout_timeout+ while every slot was
194
- # held. No Sandbox state is touched — retrying succeeds as soon as a holder
195
- # returns its Sandbox.
118
+ # Pool#with waited past +checkout_timeout+ with every slot held. Retrying
119
+ # succeeds once a holder returns its Sandbox.
196
120
  class PoolTimeoutError < Error; end
197
121
  end
@@ -4,25 +4,20 @@ require_relative "capture"
4
4
  require_relative "usage"
5
5
 
6
6
  module Kobako
7
- # Kobako::Execution — the frozen result of one +Sandbox#eval+ / +#run+: the
8
- # decoded +#value+ plus the run's output captures and +#usage+. A successful
9
- # run returns it; a failed run raises an error carrying the same frozen
10
- # Execution on the error's +#execution+, so a rescue reads the captures and
11
- # usage exactly as a successful caller reads them off the return value. On a
12
- # failed run +#value+ is +nil+ — only the captures and usage are meaningful.
7
+ # The record of one Sandbox#eval or Sandbox#run: the guest's #value, its
8
+ # output, and its #usage. A failed run raises instead, and the error carries
9
+ # the same record on +execution+, where #value is +nil+.
13
10
  #
14
- # +#failed?+ tells the returned Execution from the carried one even when both
15
- # +#value+ are +nil+ (a run whose last expression was +nil+ versus a failed
16
- # one). It holds no reference to the raising error, so the error carries the
17
- # Execution but not the reverse.
11
+ # #failed? tells the two apart when both #value are +nil+: a script whose
12
+ # last expression was +nil+ versus one that failed.
18
13
  class Execution
19
- # The deserialized guest value the run produced; +nil+ on a failed run.
14
+ # The guest value the run produced; +nil+ on a failed run.
20
15
  attr_reader :value
21
16
 
22
- # The +Kobako::Usage+ resource accounting for this run.
17
+ # What the run spent against its caps, as a Usage.
23
18
  attr_reader :usage
24
19
 
25
- def initialize(value:, usage:, stdout:, stderr:, failed:)
20
+ def initialize(value:, usage:, stdout:, stderr:, failed:) # :nodoc:
26
21
  @value = value
27
22
  @usage = usage
28
23
  @stdout_capture = stdout
@@ -31,22 +26,22 @@ module Kobako
31
26
  freeze
32
27
  end
33
28
 
34
- # Returns +true+ iff the run failed — +false+ on the Execution +#eval+ /
35
- # +#run+ returned, +true+ on the one a raised error carries.
29
+ # Whether the run failed: +false+ on the Execution #eval or #run
30
+ # returned, +true+ on the one a raised error carries.
36
31
  def failed? = @failed
37
32
 
38
- # Bytes the guest wrote to stdout during this run as a UTF-8 String,
39
- # clipped at +stdout_limit+; the content carries no truncation sentinel,
40
- # so use +#stdout_truncated?+ to observe overflow.
33
+ # What the guest wrote to stdout, up to +stdout_limit+ bytes: a UTF-8
34
+ # String, or a binary one when the bytes are not valid UTF-8. The text
35
+ # carries no sign it was cut; #stdout_truncated? says so.
41
36
  def stdout = @stdout_capture.bytes
42
37
 
43
- # Bytes the guest wrote to stderr during this run. Mirror of #stdout.
38
+ # What the guest wrote to stderr; see #stdout.
44
39
  def stderr = @stderr_capture.bytes
45
40
 
46
- # Returns +true+ iff stdout capture reached +stdout_limit+ during this run.
41
+ # Whether stdout reached +stdout_limit+.
47
42
  def stdout_truncated? = @stdout_capture.truncated?
48
43
 
49
- # Returns +true+ iff stderr capture reached +stderr_limit+ during this run.
44
+ # Whether stderr reached +stderr_limit+.
50
45
  def stderr_truncated? = @stderr_capture.truncated?
51
46
  end
52
47
  end
@@ -1,48 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kobako
4
- # Kobako::Extension — a guest idiom paired with an optional host backend,
5
- # installed on a Sandbox via +Sandbox#install+. It composes the existing
6
- # +#preload+ (the guest +source+) and +#bind+ (the +backend+) verbs into
7
- # one setup unit, so guest code sees a native-style constant whose pure
8
- # methods run in-guest and whose privileged methods dispatch to the
9
- # backend.
4
+ # Guest source paired with an optional host backend, installed with
5
+ # Sandbox#install: the guest sees one constant whose pure methods run in the
6
+ # guest and whose privileged ones call the backend. Any object answering the
7
+ # same four readers installs the same way.
10
8
  #
11
- # The four readers form the contract +#install+ duck-types on:
12
- #
13
- # * +name+ — a Symbol matching +/\A[A-Z]\w*\z/+, the preloaded snippet's
14
- # canonical backtrace name and the +depends_on+ match key. Independent
15
- # of any bound path.
16
- # * +source+ — the mruby idiom as a String; always present, since an
17
- # Extension always carries a guest idiom. A host object with no idiom
18
- # is bound with +#bind+ directly.
19
- # * +backend+ — an +Extension::Backend+ or +nil+ for a pure-guest
20
- # Extension.
21
- # * +depends_on+ — Symbol names of Extensions that must also be
22
- # installed, checked for presence at the first invocation.
23
- #
24
- # +Kobako::Extension+ is the bundled value type; any object exposing the
25
- # four readers is equally valid, so a Host App or gem may supply its own.
9
+ # [+name+] A constant-form Symbol: the snippet's backtrace name, and
10
+ # what another Extension's +depends_on+ names.
11
+ # [+source+] The mruby source, a String. A host object with no source
12
+ # is bound with Sandbox#bind instead.
13
+ # [+backend+] A Backend, or +nil+ for an Extension that runs entirely in
14
+ # the guest.
15
+ # [+depends_on+] Names of Extensions that must also be installed, checked
16
+ # at the first invocation.
26
17
  class Extension < Data.define(:name, :source, :backend, :depends_on)
27
- # Kobako::Extension::Backend — the host attachment of an Extension,
28
- # pairing +path+ (the constant path the backend binds at, single-segment
29
- # +"File"+ or nested +"MyApp::Store"+, spelling the guest constant the
30
- # idiom routes to) with the source of the bound object, declared by
31
- # explicit keyword:
18
+ # The host object an Extension's constant at +path+ calls, such as
19
+ # <tt>"MyApp::Store"</tt>, given by keyword:
32
20
  #
33
- # * +object:+ — a static object, fixed for the Sandbox's life.
34
- # * +provider:+ — a no-argument callable invoked once per invocation to
35
- # yield that invocation's object, so a fresh object backs the path
36
- # every invocation. A provider that raises propagates its exception to
37
- # the invocation caller and leaves the guest unrun; the next
38
- # invocation resolves it afresh.
39
- # * neither — a fillable, defaulting to +Kobako::Unresolved+ until the
40
- # host supplies the invocation's object.
21
+ # [+object:+] One object for the Sandbox's life.
22
+ # [+provider:+] A callable taking no arguments, called once per invocation
23
+ # for that invocation's object. If it raises, the exception
24
+ # reaches the caller and the guest does not run.
25
+ # [neither] A fillable Service, supplied per invocation through
26
+ # Context#bind.
41
27
  #
42
- # The kind is chosen by keyword, never inferred from whether the value is
43
- # callable, so a static object that is itself callable is expressed
44
- # directly with +object:+. Giving both +object:+ and +provider:+ raises
45
- # +ArgumentError+.
28
+ # The keyword decides the kind, so a callable object meant as itself is
29
+ # given as +object:+. Giving both raises ArgumentError.
46
30
  class Backend < Data.define(:path, :object, :provider)
47
31
  def initialize(path:, object: nil, provider: nil)
48
32
  if !object.nil? && !provider.nil?
@@ -54,8 +38,7 @@ module Kobako
54
38
  end
55
39
  end
56
40
 
57
- # +backend+ and +depends_on+ default to absent so the common
58
- # pure-idiom and single-backend shapes stay terse.
41
+ # +backend+ defaults to none, and +depends_on+ to no other Extension.
59
42
  def initialize(name:, source:, backend: nil, depends_on: [])
60
43
  super
61
44
  end
data/lib/kobako/handle.rb CHANGED
@@ -1,19 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kobako
4
- # A Capability Handle as it crosses the wire (ext 0x01, laid out in
5
- # {docs/wire-codec.md}[link:../../docs/wire-codec.md]), in either
6
- # direction. Host App code cannot construct one: +.new+ is private and
7
- # +#with+ removed, so no Handle is ever made from a caller-chosen id.
8
- # The guest's +Kobako::Handle+ shares only the name.
4
+ # A guest-held reference to a host object, named by id. Host code cannot
5
+ # construct one, so no Handle is ever made from a caller-chosen id.
9
6
  class Handle < Data.define(:id)
10
- # ID 0 is the invalid sentinel and is never allocated.
7
+ # The smallest id; 0 never names a Handle.
11
8
  MIN_ID = 1
12
- # The positive half of a signed 32-bit integer, so an id fits either
13
- # side of the wire unchanged.
9
+ # The largest id, the positive half of a signed 32-bit integer; an
10
+ # invocation needing more raises HandleExhaustedError.
14
11
  MAX_ID = 0x7fff_ffff
15
12
 
16
- def initialize(id:)
13
+ def initialize(id:) # :nodoc:
17
14
  raise ArgumentError, "Handle id must be Integer" unless id.is_a?(Integer)
18
15
  raise ArgumentError, "Handle id #{id} out of range [#{MIN_ID}, #{MAX_ID}]" unless id.between?(MIN_ID, MAX_ID)
19
16
 
@@ -23,9 +20,7 @@ module Kobako
23
20
  private_class_method :new
24
21
  undef_method :with
25
22
 
26
- # The Host Gem's own constructor, for the wire decoder and the Handle
27
- # allocator; it runs the same id checks +.new+ would.
28
- def self.restore(id)
23
+ def self.restore(id) # :nodoc:
29
24
  allocate.tap { |handle| handle.send(:initialize, id: id) }
30
25
  end
31
26
  end
@@ -4,15 +4,7 @@ require_relative "codec"
4
4
  require_relative "transport/error"
5
5
 
6
6
  module Kobako
7
- # Host-facing boundary for the invocation outcome the native side split
8
- # off the core envelope. Takes the arm it named plus the fields that arm
9
- # carries, and settles the invocation the way a host does: return the
10
- # value, or raise the exception the failure attributes to.
11
- #
12
- # This is the two-step attribution decision. The wire framing belongs to
13
- # the native side; the payload codec at +Kobako::Codec+ decodes only
14
- # the one arm that carries a value.
15
- module Outcome
7
+ module Outcome # :nodoc:
16
8
  # The two +origin+ values a Panic attributes with.
17
9
  ORIGIN_SANDBOX = "sandbox"
18
10
  ORIGIN_SERVICE = "service"
@@ -24,91 +16,70 @@ module Kobako
24
16
  SUBCLASSES = {
25
17
  "Kobako::BytecodeError" => BytecodeError,
26
18
  "Kobako::UndefinedEntrypointError" => UndefinedEntrypointError,
19
+ "Kobako::UnsupportedFlagError" => UnsupportedFlagError,
27
20
  "Kobako::Transport::Error" => Kobako::Transport::Error,
28
21
  "Kobako::NoServiceError" => NoServiceError,
29
22
  "Kobako::ServiceArgumentError" => ServiceArgumentError
30
23
  }.freeze
24
+ private_constant :ORIGIN_SANDBOX, :ORIGIN_SERVICE, :SUBCLASSES
31
25
 
32
- module_function
33
-
34
- # Settle one invocation. +kind+ names the arm, +payload+ is the
35
- # codec-encoded value the +:ok+ arm carries, and +panic+ carries
36
- # the Panic's fields +[origin, class, message, backtrace, available]+ —
37
- # present on the panic arm and absent on every other, which is what
38
- # tells the failure that has a record to attribute from apart from the
39
- # two that do not. +entrypoint+ is the name this invocation asked for,
40
- # which the host knows and the wire therefore never carries.
41
- def reify(kind, payload, panic, entrypoint: nil)
42
- return decode_value(payload) if kind == :ok
26
+ # +panic+ is present only on the panic arm, which is what tells it from
27
+ # the ok arm's +payload+. +entrypoint+ is known to the host and never
28
+ # carried by the wire.
29
+ def self.reify(payload, panic, entrypoint: nil)
30
+ return decode_value(payload) unless panic
43
31
 
44
- raise panic ? panic_error(panic, entrypoint) : trap_error(kind)
32
+ raise panic_error(panic, entrypoint)
45
33
  end
46
34
 
47
- # Map a Panic's fields onto the three-layer taxonomy. The fields land
48
- # on the exception verbatim — it carries the record rather than a
49
- # translation of one.
50
- def panic_error(panic, entrypoint)
51
- origin, klass, message, backtrace, available = panic
52
- selected = error_class(origin, klass)
53
- attribution = { origin: origin, klass: klass, backtrace_lines: backtrace }
54
- return selected.new(message, **attribution) unless selected == UndefinedEntrypointError
35
+ # +reify+ is the whole seam: one call settles one invocation, matching
36
+ # the single entry point the Rust frontend's twin exposes. Everything
37
+ # else here is how that decision is reached.
38
+ class << self
39
+ private
55
40
 
56
- UndefinedEntrypointError.new(message, name: entrypoint, available: available.map(&:to_sym), **attribution)
57
- end
41
+ # The fields land on the exception verbatim — it carries the record
42
+ # rather than a translation of one.
43
+ def panic_error(panic, entrypoint)
44
+ origin, klass, message, backtrace, available = panic
45
+ selected = error_class(origin, klass)
46
+ attribution = { origin: origin, klass: klass, backtrace_lines: backtrace }
47
+ return selected.new(message, **attribution) unless selected == UndefinedEntrypointError
58
48
 
59
- # +origin+ picks the branch, and the guest-written class name may
60
- # narrow within it so callers can rescue one path specifically. A name
61
- # naming a class outside the branch its origin chose is ignored rather
62
- # than honoured: what a guest calls its exception must not move the
63
- # failure to a layer the attribution did not put it in.
64
- def error_class(origin, klass)
65
- base = origin == ORIGIN_SERVICE ? ServiceError : SandboxError
66
- selected = SUBCLASSES.fetch(klass, base)
67
- return base unless selected <= base
49
+ UndefinedEntrypointError.new(message, name: entrypoint, available: available.map(&:to_sym), **attribution)
50
+ end
68
51
 
69
- selected
70
- end
52
+ # +origin+ picks the branch, and the guest-written class name may
53
+ # narrow within it so callers can rescue one path specifically. A name
54
+ # naming a class outside the branch its origin chose is ignored rather
55
+ # than honoured: what a guest calls its exception must not move the
56
+ # failure to a layer the attribution did not put it in.
57
+ def error_class(origin, klass)
58
+ base = origin == ORIGIN_SERVICE ? ServiceError : SandboxError
59
+ selected = SUBCLASSES.fetch(klass, base)
60
+ return base unless selected <= base
71
61
 
72
- # An arm the host cannot settle: the guest wrote nothing, or wrote
73
- # bytes the envelope cannot frame. Either leaves nothing to attribute
74
- # to, so both walk the trap path; only the absent-versus-present
75
- # distinction selects the message.
76
- def trap_error(kind)
77
- return TrapError.new("Sandbox exited without producing a result") if kind == :absent
62
+ selected
63
+ end
78
64
 
79
- TrapError.new(
80
- "Sandbox produced an unrecognised result; the runtime is corrupted, " \
81
- "discard this Sandbox before another invocation"
82
- )
83
- end
65
+ # A decode fault means the framing was fine but the carried value is
66
+ # unrepresentable.
67
+ def decode_value(payload)
68
+ Kobako::Codec::Decoder.decode(payload)
69
+ rescue Kobako::Codec::Error => e
70
+ raise wire_error("Sandbox produced an invalid result value", diagnostic: e.message)
71
+ end
84
72
 
85
- # The ok arm's value — the one position a payload codec still
86
- # owns on this path. A decode fault means the framing was fine but the
87
- # carried value is unrepresentable.
88
- def decode_value(payload)
89
- Kobako::Codec::Decoder.decode(payload)
90
- rescue Kobako::Codec::Error => e
91
- raise wire_error("Sandbox produced an invalid result value", diagnostic: e.message)
73
+ # No guest class stands behind a result kobako cannot decode, so
74
+ # +klass+ names the host class raised.
75
+ def wire_error(message, diagnostic: nil)
76
+ Kobako::Transport::Error.new(
77
+ message,
78
+ origin: ORIGIN_SANDBOX,
79
+ klass: "Kobako::Transport::Error",
80
+ diagnostic: diagnostic
81
+ )
82
+ end
92
83
  end
93
-
94
- # Lift a wire violation the host detected to the real
95
- # +Kobako::Transport::Error+ class so callers can +rescue+ it
96
- # specifically instead of pattern-matching on +error.klass+. The
97
- # +klass+ field is still populated so existing operator-side tooling
98
- # that greps on the string continues to work.
99
- def wire_error(message, diagnostic: nil)
100
- Kobako::Transport::Error.new(
101
- message,
102
- origin: ORIGIN_SANDBOX,
103
- klass: "Kobako::Transport::Error",
104
- diagnostic: diagnostic
105
- )
106
- end
107
-
108
- # +reify+ is the whole seam: one call settles one invocation, matching
109
- # the single entry point the Rust frontend's twin exposes. Everything
110
- # else here is how that decision is reached.
111
- private_class_method :panic_error, :error_class, :trap_error, :decode_value, :wire_error
112
- private_constant :ORIGIN_SANDBOX, :ORIGIN_SERVICE, :SUBCLASSES
113
84
  end
114
85
  end
@@ -4,19 +4,7 @@ require_relative "../codec"
4
4
 
5
5
  module Kobako
6
6
  module Payload
7
- # The invocation arguments a Call or a Run carries: a 2-element
8
- # msgpack array, +args+ then +kwargs+. Both elements are always
9
- # present, so field positions stay stable when either is empty.
10
- #
11
- # The positional-versus-keyword split lives here rather than in the
12
- # core envelope because it is Ruby's call semantics, not the wire's.
13
- # SPEC pins +kwargs+ keys to Symbols; the invariant is enforced at
14
- # construction so the value object is the single source of truth.
15
- #
16
- # Built on the +class X < Data.define(...)+ subclass form so the class
17
- # body is fully Steep-visible; see +.rubocop.yml+ for the
18
- # rationale.
19
- class Arguments < Data.define(:args, :kwargs)
7
+ class Arguments < Data.define(:args, :kwargs) # :nodoc:
20
8
  def initialize(args: [], kwargs: {})
21
9
  raise ArgumentError, "payload args must be Array" unless args.is_a?(Array)
22
10
 
@@ -30,9 +18,6 @@ module Kobako
30
18
  Codec::Encoder.encode([args, kwargs])
31
19
  end
32
20
 
33
- # Decode +bytes+ into an Arguments. Raises +Codec::InvalidTypeError+ when
34
- # the payload is not the expected 2-element msgpack array, or when the
35
- # construction invariants reject the decoded fields.
36
21
  def self.decode(bytes)
37
22
  Codec::Decoder.decode(bytes) do |frame|
38
23
  unless frame.is_a?(Array) && frame.length == 2